Why does my script loop like this?

So I am making a game sorta like bear, whereas when a player gets hurt, gui pops up on the screen like this. But it loops, and I don’t know why, here is the video + script.


It loops whenever I get hurt while the hurt GUI (idk what its called) is playing, here is the script:

workspace[game.Players.LocalPlayer.Name]:WaitForChild("Humanoid")
label = script.Parent.TextLabel
debounce = true
local function hurt()
	if debounce == true then
		debounce = false
		local num = math.random(1,3)
		if num == 1 then
			label.Text = "BEHIND YOU"
		elseif num == 2 then
			label.Text = "RUN"
		elseif num == 3 then
			label.Text = "ITS HERE"
		end
		script.Parent.LocalScript.Disabled = false
		
		local num = 0
		while wait(.01) do
			local clone = game.ReplicatedStorage.special:Clone()
			clone.Parent=script.Parent
			num = num + .01
			if num >= .75 then
				script.Parent.TextLabel.Text = ""
				break
			end
		end
		
		debounce = true
	end
end	
workspace[game.Players.LocalPlayer.Name].Humanoid:GetPropertyChangedSignal('Health'):Connect(hurt)

It loops for about a minute

1 Like

Try making it so it waits a little before activating the debounce:

workspace[game.Players.LocalPlayer.Name]:WaitForChild("Humanoid")
label = script.Parent.TextLabel
debounce = true
local function hurt()
	if debounce == true then
		debounce = false
		local num = math.random(1,3)
		if num == 1 then
			label.Text = "BEHIND YOU"
		elseif num == 2 then
			label.Text = "RUN"
		elseif num == 3 then
			label.Text = "ITS HERE"
		end
		script.Parent.LocalScript.Disabled = false
		
		local num = 0
		while wait(.01) do
			local clone = game.ReplicatedStorage.special:Clone()
			clone.Parent=script.Parent
			num = num + .01
			if num >= .75 then
				script.Parent.TextLabel.Text = ""
				break
			end
		end
		
		wait(1)
		debounce = true
	end
end	
workspace[game.Players.LocalPlayer.Name].Humanoid:GetPropertyChangedSignal('Health'):Connect(hurt)
1 Like

still does the same thing execpt it waits a second (Ive set it like that) to do the next one, so still loops, only slower

edit: I had a wait on there before, but it looks like I deleted it. not sure why, but I probably deleted it when I moved a function that was above the main code to inside of it.

maybe add a debounce that should do the trick oh wait nvm

isnt there already one? or am I missing something. . .

nvm

wait you already did add a debounce

no no i looked at the script i just didnt see the debounce

hmmm i have no idea yet if you hit it one time it stops but if you hit it two times it loops like that? hold on

nvm, it just loops no matter if I hit the damage brick once or twice

have you check if the connections are disconnected

Sorry, but what are you asking?

well usually when you use connect you also disconnect correct?

when i usually connect to what?

Okay when you use

:Connect you also break the connection or disconnect but it seems you already did that hmmm

You put the debounce the wrong way around. You gotta put it on false instead of true

It checks the value the same way as if it were false to false.

wait how come i didnt see that

so it looks like what i set the wait for the debounce to come back on (?) is the wait for the loop to start again, so for example if I set it for 4 seconds, it would:
do it once, wait four second, then do it again, rinse and repeat approx. 6 times

It might be the way you call the function, try using a different calling method.

well try what the guy said set debounce to true