• Reduce Hunger over time
    4 replies, posted
I want to make a script where the hunger is reduced overtime. I tried networks. Also if you need some code well here you go then. function GM:PlayerInitialSpawn(ply) ply:SetGravity(1) ply:SetWalkSpeed(320) ply:SetRunSpeed(320) ply:SetCrouchedWalkSpeed(0.3) ply:SetDuckSpeed(0.5) ply:SetNoCollideWithTeammates(false) ply:SetModel("models/player/Group01/Male_01.mdl") ply:SetJumpPower(0) --Set Values ply:SetNWInt("PlayerSimolions", 1000) ply:SetNWInt("PlayerHunger", 100) print(ply:Nick() .. " joined") end Draw Hunger     --Draw Hunger     surface.SetDrawColor(50,50,50,255) -- Black     surface.DrawRect(ScrW() - 780,20,220,20)     surface.SetDrawColor(120,139,115,255) -- Green     surface.DrawRect(ScrW() - 780,20,220*(ply:GetNWInt("PlayerHunger",0)/100),20)     --Draw Text "Hunger"     surface.SetFont("CloseCaption_Normal")     surface.SetTextColor(0,0,0,255)     surface.SetTextPos(ScrW() - 780,15)     surface.DrawText("Hunger")
Either create a timer for the player on their initial spawn and call it every second or whatever then just decrease it? Or use tick hook, curtime & decrease there.
--Set Initial Spawn Stuff function GM:PlayerInitialSpawn(ply) ply:SetGravity(1) ply:SetWalkSpeed(320) ply:SetRunSpeed(320) ply:SetCrouchedWalkSpeed(0.3) ply:SetDuckSpeed(0.5) ply:SetNoCollideWithTeammates(false) ply:SetModel("models/player/Group01/Male_01.mdl") ply:SetJumpPower(0) --Set Values ply:SetNWInt("PlayerSimolions", 1000) ply:SetNWInt("PlayerHunger", 100) print(ply:Nick() .. " joined") timer.Create("HungerTimer",3,0,function(ply) ply:SetNWInt("PlayerHunger", ply:GetNWInt("PlayerHunger",0) - 1) end) end [ERROR] gamemodes/gsims/gamemode/init.lua:20: attempt to index local 'ply' (a nil value)   1. unknown - gamemodes/gsims/gamemode/init.lua:20 Timer Failed! [HungerTimer][@gamemodes/gsims/gamemode/init.lua (line 20)] Error when it trys to run
Well, the error is rather self explanatory. You are declaring 'ply' to be an argument of the timer function, instead of using an upvalue. function GM:PlayerInitialSpawn(ply) -- 'ply' is an argument of this function, used as a hook. The value is passed by hook.Call local thePlayer = ply timer.Create(..., function(ply) -- The hook's 'ply' is "shadowed" by your declaration of 'ply' as a function parameter. -- Timer functions have no parameters, that is why 'ply' is nil inside here -- You can still reference the outside 'ply' if you don't declare the function parameter, or use 'thePlayer' in here. end) end An other thing to keep in mind, is that if you create a named timer, it will be overriden by an other one if you create it with the same name. So instead of making a timer per-player, create one that iterates over all players, and manages their hunger: timer.Create("ManagePlayerHunger", ..., function() for index, player in ipairs(players.GetAll()) do -- Code inside here will be ran for all players on the server player:SetNWInt("PlayerHunger", player:GetNWInt("PlayerHunger", 0) - 1) end end)
THANK YOU
Sorry, you need to Log In to post a reply to this thread.