Hi. I am trying to do a Thirst and Hunger script.
This is inside my script:
game.Players.PlayerAdded:connect(function(player) while wait() do wait(20) script.SendSignal:FireClient(player) end end
I have tried without player added, and without the player argument, and it didn't work. Then I tried with the player added, and nothing happened. I don't know how to define the player.
LocalScript:
local SSS = game:GetService('ServerScriptService') local SendSignal = SSS.HungerScript.SendSignal local player = game:GetService('Players').LocalPlayer SendSignal.OnClientEvent:connect(function() if script.Parent.Hunger.Value >= 0 then script.Parent.Hunger.Value = script.Parent.Hunger.Value -1 end if script.Parent.Hunger.Value <= 0 then repeat wait(5) player.Character.Humanoid.Health = player.Character.Humanoid.Health -2 until script.Parent.Hunger > 0 end end)
Local scripts cannot see inside ServerScriptService or ServerStorage. Instead, all RemoteEvents and RemoteFunctions should be stored inside ReplicatedStorage.
You should put the remote event in ReplicatedStorage and do it like this instead:
game.Players.PlayerAdded:connect(function(player) while wait() do wait(20) game.ReplicatedStorage.SendSignal:FireClient(player) end end
local RS = game.ReplicatedStorage local SendSignal = RS.SendSignal local player = game:GetService('Players').LocalPlayer SendSignal.OnClientEvent:connect(function() if script.Parent.Hunger.Value >= 0 then script.Parent.Hunger.Value = script.Parent.Hunger.Value -1 end if script.Parent.Hunger.Value <= 0 then repeat wait(5) player.Character.Humanoid.Health = player.Character.Humanoid.Health -2 until script.Parent.Hunger > 0 end end)
As well as this, sending a signal over to the client at regular intervals may take up unnecessary bandwidth. It shouldn’t be too much of a problem with intervals of that length, but a better practice may be to have the loop on the client instead of the server.