Loop-reduce health code is not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to make a script that reduces my health in a loop when i jump into the water and stop reducing my health when im out of the water

  2. What is the issue? Include screenshots / videos if possible!
    the code below is not killing me in a certain time

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i didnt

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local lava = script.Parent

local isTouched = false

local function killPlayer()
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChild("Humanoid")
	for count = 1, 10 do
		humanoid.Health = count / 20
	end
end

2 Likes

Try this :

local Lava = script.Parent
local Debounce = false
local function killPlayer(Object)
	if Object.Parent:FindFirstChild("Humanoid") then
		if Debounce == false then
			Debounce = true
			Object.Parent.Humanoid.Health -= 5
			task.wait(2)
			Debounce = false
		end
	end
end

Lava.Touched:Connect(killPlayer)
2 Likes

A few errors I noticed in your code :

1- You forgot to put ‘otherPart’ as an argument in the fuction killPlayer()
2- I don’t see any call to the function
3-When using for loops, you should add a delay [doesn’t have to be too long]

2 Likes

thanks, i want the player’s health to be zero when 5 second have elapsed an the timer (the time limit) to reset when the player doesn’t touch the part

(marking your first post as solution for now

2 Likes

You could use tick() or time.os() to check how many seconds have elapsed since something happend,

glad I could help :slight_smile:

3 Likes

hey, i found an issue where only when i jump in the water, it does not loop-reduce health