Defining player health

You can’t really get the LocalPlayer from server side scripts. And Also if you change the health value for a player from Server Side & update something in RS it gets updated for everyone, which means its not good for Multiplayer games. There are few more issues with your code, which other people have stated too.

So what you should do is something like this instead.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) --Everytime a player joins, you make a health value in the player instance & start the Loop.
    local FoodStatus = Instance.new("NumberValue", player) --New Numbervalue which stores the FoodStatus, and parent it to the Player instance.
    FoodStatus.Value = 100 --Set it to 100 by default

   --Then we use a CharacterAdded function because the humanoid won't exist till Character is Added first.
    player.CharacterAdded:Connect(function(char) --Get the new character model
       local Humanoid = char:WaitForChild("Humanoid") --Wait For the Humanoid To Load in

       --The Loop is here again.
       while wait(1.6) do
	    FoodStatus.Value -=1
	     if FoodStatus.Value < 1 then
		   Humanoid.health -=1
	     elseif FoodStatus.Value < -30 then
		   Humanoid.health -=1.5
	     elseif FoodStatus.Value < -50 then
		    Humanoid.health -=2.5       
	     end
       end
   end)
end)

This is just an example of how you could do it :slight_smile:

2 Likes