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
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.
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.
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!