Restart timer after converted content in TextLabel changes (Bleed System)

  1. What do you want to achieve?
    It’s simple, but I am out of ideas. I want to restart a loop/the timer everytime the Text from the TextLabel updates. I am converting the string into a number so that won’t be the problem.

Description:
Everytime a player hits another player, an event (Bleed) is fired and connects in this script where it increases the number of bleed by 0.5 which works, and makes the bleed icon visible. Moreover I want the bleeding to stop after the amount of bleed decreases by one until it hits 0. If it hit zero the timer/loop will break, and the icon becomes invisible.

  1. What is the issue?
    The timer does not reset properly, since 10 is currently set as default it just counts down from 10. After 10 seconds the icon disappears. Though I want it so if the player is being hit and the number next to the icon increases by or ends at let’s say 18, I want the timer to count down from 18 and then make the icon invisible.

  2. What solutions have you tried so far?
    YouTube, other DevForum posts

What it looks like:
Screenshot (3927)

 -- Increase Bleed ---
game.ReplicatedStorage.Bleed.OnClientEvent:Connect(function()
	script.Parent.Visible = true
	BleedEnabled()
end)

--Functions --
function BleedEnabled()
	script.Parent.Amount.Text = tonumber(script.Parent.Amount.Text) + 0.5
	
	local BleedAmount = tonumber(script.Parent.Amount.Text)

	local player = game.Players.LocalPlayer
	local hum = game.Workspace:WaitForChild("Characters"):WaitForChild(player.Name):WaitForChild("Humanoid")

	local timer = BleedAmount;

	coroutine.wrap(function()
		while hum.Health > 0 do -- loop until the player dies
			repeat
				wait(1);
				timer -= 1;
			until timer <= 0;
			script.Parent.Visible = false
			timer = BleedAmount;
		end
	end)()


	script.Parent.Amount:GetPropertyChangedSignal("Text"):Connect(function()
		local NewTimer = tonumber(script.Parent.Amount.Text)
		timer = BleedAmount


		if BleedAmount >= 0 then
			script.Parent.Visible = true
		end 
	end)
end

If any of you have any questions, feel free to ask as I might have been unclear about what I want to achieve. Any help is appreciated.

1 Like

Does nobody have an idea how to fix this?

1 Like

Maybe replace it with this, I don’t know if it will help, or if it’s what you’re looking for.


script.Parent.Amount:GetPropertyChangedSignal("Text"):Connect(function()
        local NewTimer = tonumber(script.Parent.Amount.Text)
		timer = NewTimer


		if timer >= 0 then
			script.Parent.Visible = true
		end 

end)

repeat
     wait(1);
     timer -= 1;

until hum.Health <= 0 or timer <= 0
script.Parent.Visible = false

		


		
	
1 Like

Oh wow it works!! Thank you so much <3

There’s just one little thing, how do I prevent the timer to go into negative numbers, because I testplayed which works but yeah the timer continues

1 Like

I don’t know, could you send me the server script that gets rid of the health?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.