Player taking damage over time instead of dying instantly

Am pretty new, I want the player to take damage over time instead of dying instantly. I don’t know how to code it, I tried messing around with it a can’t figure it out.

Here is part of the code I want to do it.:


		
		if Hunger.Value <= 0 then
			Player.Character:BreakJoints()
			else

What you are doing there is making it so every time the Hunger value is less than 0 it will kill the character.

This is what you would do to achieve it:

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
  Player.Character.Humanoid.Health = Hunger.Value
end)

Basically what this does is it will always loop setting the player health what the hunger value is.

You could have a script that just makes the player take damage if the hunger value is 0 which would look like


while wait(5) do -- every 5 seconds
    if Hunger.Value <= 0 then
         Player.Character.Humanoid:TakeDamage(5) -- Take 5 Damage
    end
end

Every 5 seconds if the hunger value is 0 they will start to take damage until they die or the hunger value goes above 0.

Never use while do its not a good practice for loops. Also Hunger.Value <= 0 won’t work since it would be tracking the negatives. :TakeDamage() in this case wouldn’t work since if Hunger is 100 it would take 100 damage.

Try this.

game:GetService("Players").PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()

	if player.Hunger:GetPropertyChangedSignal("Value") then
		if player.Hunger.Value < 0 then
			character.Humanoid:TakeDamage(100)
		end
	end
end)

It’s better to do <= 0 than == 0 here because depending on how hunger is calculated & due to the way decimals work it’s possible that because of the nature of precision errors, 0 is never actually hit specifically, but instead skipped past resulting in a negative hunger stat.

local DECREASE_AMOUNT = 10
local Pizza = 20

game:GetService("Players").PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()

	local Hunger = Instance.new("IntValue", player)
	Hunger.Name = "Hunger"
	Hunger.Value = 100
	
	
	if player.Hunger:GetPropertyChangedSignal("Value") then
		if player.Hunger.Value < 0 then
			character.Humanoid:TakeDamage(100)
			else
			while wait(DECREASE_FREQUENCY) do
	Hunger.Value -= DECREASE_AMOUNT
	    end
	 end
      end
end)

game.ReplicatedStorage.Eat.OnServerEvent:Connect(function(player)
	player.Hunger.Value += Pizza
end)
``` Try this, contact me if any problem and i'll try to help you.

When it hits zero nothing happens you won’t take damage also I don’t know if this was in the script or not I want the make the player take damage over time.

game:GetService("Players").PlayerAdded:Connect(function(Player)
	local Hunger = Instance.new("IntValue")
	Hunger.Parent = Player
	Hunger.Name = "Hunger"
	
	local threshold = 50
	local amount = 3
	local interval = 4
	local damaged = false
	
	Hunger:GetPropertyChangedSignal("Value"):Connect(function()
		
		if Hunger.Value <= 50 and not damaged then
			if Player.Character and Player.Character:FindFirstChild("Humanoid") then
				Player.Character:FindFirstChild("Humanoid"):TakeDamage(amount)
				damaged = true
			end
			wait(interval)
			damaged = false
		end
			
	end)
end)

The whole thing doesn’t work the local values work with the script. The bar just disappears and nothing works I can give you the hunger bar code if you need it. Am sorry if this is a pain.

Okay give me the hunger bar code

Wait where are you putting this script?

Also if you can change the code so the bar goes down instead of sideways that would be helpful.

game:GetService("Players").PlayerAdded:Connect(function(Player)
    while Hunger.Value >= 0 do
        Player.Character.Humanoid:TakeDamage(5)
        wait(1)
    end 
end)

Put this in a serverscript in serverscriptservice and test it

I think what you did wrong was the Hunger.Value <= 0 then part.
This is how I think you can fix it:

local DECREASE_FREQUENCY = 1
local DECREASE_AMOUNT = 10
local Pizza = 20
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer:CharacterAdded:Wait()

game.Players.PlayerAdded:Connect(function(Player)
	
	
	local Hunger = Instance.new("IntValue", Player)
	Hunger.Name = "Hunger"
	Hunger.Value = 100
	
	Player.CharacterAdded:Connect(function()
		Hunger.Value = 100
	end)
	
	while wait(DECREASE_FREQUENCY) do
		
		if Hunger.Value = 0 then
			character.Humanoid.Health = 0
		else
		
		Hunger.Value -= DECREASE_AMOUNT
			end
		end
end)

game.ReplicatedStorage.Eat.OnServerEvent:Connect(function(Player)
	Player.Hunger.Value = Player.Hunger.Value + Pizza
end)

I have not tested this so I cannot gurantee this works, also I am not good in stats, and other stuff, so I have no Idea if I did any of that syntax right. Thanks!

No, it’s not. it’s going into the negatives it could be on my side though. the bar works but the player isn’t taking damage.

Something finally snap in my brain I used part of this script to fix it thank you.