Defining player health

Hi all, I’m trying to change the health of the player (sss script), but im failing to define it :expressionless: (sorry if im being really dumb) my script:

local rs = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local health = game.Workspace[player.Name]:FindFirstChild("Humanoid")
local FoodStatus = rs.FoodStatus

while wait(1.6) do
	FoodStatus.Value -=1
	if FoodStatus.Value < 1 then
		health -=1
	elseif FoodStatus.Value < -30 then
		health -=1.5
	elseif FoodStatus.Value < -50 then
		health -=2.5 
	end
end

Thanks for reading

You can’t access LocalPlayer in a ss. If you want to do it for everyone, make a for loop.

Then how should I do this? [30]

Loop through every player in the game with a pairs (for loop)

omg i think i just realised i shouldn’t even have put my script into sss lol

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

You defined the “health” variable here as the humanoid, not its health.
To define its health now you should have to write health.Health.
also, if you put the .Health in the variable, the variable will not store an instance, it will store an int value that is the humanoid health only when the script runned first time. It will be aways 100 probably, and you cant change a unrelated int value and expect to update humanoids health. For this you will have to store only the instance humanoid (like you did above but in a variable called “health” when it would be better calling it “humanoid”) and change its health by using Variable.Health = x

1 Like