• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved Starving script, Possible?

popoverde

Intermediate OT User
Joined
Jan 16, 2015
Messages
202
Reaction score
135
Location
[RPG]
The Forgotten Server - Version 0.3 (Crying Damson).


Example: If a player dont eat in X time he start losing HP .




Thanks.
 
so I only need to add -------->

function onThink(interval, lastExecution)
for _, cid in pairs(getPlayersOnline()) do
if getPlayerFood(cid) == 0 then
doCreatureAddHealth(cid, -10)
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
end
end
return true
end

to starving lua in globalevents and register it in globalevents xml and then it will work? does it need login lua or anything?
the new scripting style for TFS is still a bit confusing to me
using 1.1 current rev
thanks
 
That is indeed a better idea.


function.lua
Code:
function doPlayerStarve(cid)
     if isPlayer(cid) == FALSE or getPlayerFood(cid) > 0 then
         return TRUE
     end
     doCreatureAddHealth(cid, -10)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
     addEvent(doPlayerStarve, 60 * 1000, cid)
     return TRUE
end

login.lua
Code:
addEvent(doPlayerStarve, 60 * 1000, cid)

food.lua (above the last return true)
Code:
addEvent(doPlayerStarve, (getPlayerFood(cid) + 60) * 1000, cid)
Use this instead.
 
Sorry for the looooong delay i got sidetracked
Using that script i get error in food lua onUse arrempt to preform arithmetic on boolean value. Traceback to addEvent. I cannot script well and would like to use this system
TFS1.1
 
It uses player
Is cid then what is refered by boolean?
Just trying to learn
I have compat lua
Does that mean no 1.1 scripts will have cid?
Thanks for helping
 
It uses player
Is cid then what is refered by boolean?
Just trying to learn
I have compat lua
Does that mean no 1.1 scripts will have cid?
Thanks for helping
In pre-1.1 servers, cid is the monster, player or npc id
In 1.0 in order to access the metamethods of the above mentioned you need pass cid to to their respective constructors.

Lets take a player for instance, if you wanted to construct a player you would pass cid to Player
Code:
local player = Player(cid)
From there you would proceed with your code and check if player is a player or is nil.
Code:
if player then
    -- do something
end
If its nil this means either cid is nil or cid is not a player.

When writing scripts you want to test all values which you are creating, returning, comparing or deleting.
A very simple way to do this is to use the print function
Code:
print(player)
This will print whatever value player holds even if player is nil to the console, if player is a table it will print table and then some arbitrary number after it.

You can also check to see what type a value is simply by using type.
Code:
print(type(player))

If you want to test more then 1 value you would place a comma after the initial value.
Code:
print(player, type(player), player:getId(), player:getName())
When writing scripts, its important to test everything :)

The compat.lua in every distro is not necessarily a compatibility file, it is a library file which simplifies code.

An example of 1.2 compat.lua
Code:
function isPlayer(cid) return Player(cid) ~= nil end
The cid you see in isPlayer is called a parameter, the value you pass to this function is called an argument, the parameter cid in isPlayer can easily be named anything, like id.
Code:
function isPlayer(id) return Player(id) ~= nil end
The parameter is not as important as the value or argument which is passed to the function, the parameter of a function/metamethod its scope or life only exists within the function definition and nowhere else.
1.0
Code:
function onStepin(cid, item slot)
      if isPlayer(cid) then -- this will call the isPlayer function from compat.lua
           -- do something
      end
end
In the movement script, cid is the argument, and will pass the value of cid to isPlayer.
 
Last edited:
That is indeed a better idea.


function.lua
Code:
function doPlayerStarve(cid)
     if isPlayer(cid) == FALSE or getPlayerFood(cid) > 0 then
         return TRUE
     end
     doCreatureAddHealth(cid, -10)
     doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "You are starving.")
     addEvent(doPlayerStarve, 60 * 1000, cid)
     return TRUE
end

login.lua
Code:
addEvent(doPlayerStarve, 60 * 1000, cid)

food.lua (above the last return true)
Code:
addEvent(doPlayerStarve, (getPlayerFood(cid) + 60) * 1000, cid)

Sorry to bump an old thread, but how would I apply this to TFS 1.2?
 
Back
Top