Damaging script heals the humanoid?

Hello I want to make a survival type game, so I decided to start out coding a thirst/hunger system. Whenever each one of these are at 0, the player will take 3 damage every 2 seconds. It somewhat works, but for some odd reason, the player gets healed (slightly), and I am confused on why. No other script changes the player’s humanoid, and it’s all handled on the server.


local datastore = game:GetService("DataStoreService")
local playerData = datastore:GetDataStore("PlayerData")
--local values = require(game.ServerScriptService.Modules.Values)

local function depleteThirst(player)
	while wait(5) and player.Values.Thirst.Value > 0 do
		player.Values.Thirst.Value -= 1
	end
end

local function depleteHunger(player)
	while wait(10) and player.Values.Hunger.Value > 0 do
		player.Values.Hunger.Value -= 1
	end
end

local function killPlayer(player)
	player.Values.Thirst.Changed:Connect(function()
		while player.Values.Hunger.Value == 0 or player.Values.Thirst.Value == 0 do
			wait(2)
			player.Character.Humanoid:TakeDamage(3)
		end
	end)
end

local depleteA = coroutine.wrap(depleteThirst)
local depleteB = coroutine.wrap(depleteHunger)
local kill = coroutine.wrap(killPlayer)

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "Values"
	folder.Parent = player

	local hunger = Instance.new("IntValue")
	hunger.Name = "Hunger"
	hunger.Parent = folder
	hunger.Value = 5

	local thirst = Instance.new("IntValue")
	thirst.Name = "Thirst"
	thirst.Parent = folder
	thirst.Value = 5

	local warmth = Instance.new("IntValue")
	warmth.Name = "Warmth"
	warmth.Parent = folder

	local stamina = Instance.new("IntValue")
	stamina.Name = "Stamina"
	stamina.Parent = folder

	depleteA(player)
	depleteB(player)
	kill(player)
end)

This is the script that handles the damaging and values,

Here is a video that explains what I mean.

https://streamable.com/prifha

(Uploaded to streamable since it errored on the devforum, also sorry for the music forgot OBS records Spotify lol).

Thanks for reading!

Notice the Health script inside of your character in the video. That is one of Roblox’s default scripts that auto-regens your health. That’s likely what is causing your problem. This can easily be solved by going under StarterPlayer > StarterCharacterScripts and inserting a disabled script named Health (insides can be left blank).

1 Like

Lol I forgot about that script, tysm!

1 Like