Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Debounce script isnt working, Its still adding multiple points? Any help?

Ive got a debounce inide a AnimationTrack.Stopped event and even with the debounce the script still fires multiple times?

Anyone got any ideas?

Heres the code

Choke.Stopped:connect(function()
	if TargetCharacter.Humanoid.Health <= 0 and KillDebounce == false then
		print("EnemyKilledYeet")
		KillDebounce = true
game.ReplicatedStorage.DataFile[Player.Name].DarthVaderKills.Value = 	game.ReplicatedStorage.DataFile[Player.Name].DarthVaderKills.Value + 1
							game.ReplicatedStorage.DataFile[Player.Name].TotalKills.Value = game.ReplicatedStorage.DataFile[Player.Name].TotalKills.Value + 1
							game.ReplicatedStorage.DataFile[Player.Name].ExperiencePoints.Value = game.ReplicatedStorage.DataFile[Player.Name].ExperiencePoints.Value + 50
		EnemyKilled:FireClient(Player)
		KillDebounce = false
	end
	for _, Parts in pairs(script.Parent:GetChildren()) do
		MainPlayer.Parent.Humanoid.WalkSpeed = 20
		MainPlayer.Parent.Humanoid.JumpPower = 75							
		if Parts and Parts:IsA("Part") or Parts:IsA("UnionOperation") or Parts:IsA("WedgePart") then
			Parts.Transparency = 0
			script.Parent.MainTrail.Trail.Enabled = true
			if Parts.Name == "Handle" then
				Parts.Transparency = 1
			end
		end
	end
end)

Im sorry about some of the long lines of code getting cut off I dont know how to fix that but basically whats happening is I use the debounce so It doesnt add more than 1 point to the stats when you get a kill but sometimes it runs 5 times and sometimes it runs only 1 time. So I dont know, the debounce script isnt working so any ideas?

Thanks so much for your time and hopefully we can solve this question :)

1 answer

✓ Accepted answer

You need to have something that yields in between KillDebounce = true and KillDebounce = false, otherwise all the other coroutines that are generated by Choke.Stopped won't run until KillDebounce = false. A simple wait() will be sufficient if the Stopped event is being fired multiple times simultaneously. Note that you might want to structure your code differently so that lines 11+ can run immediately, possibly just by changing line 9 to delay(0.01, function() KillDebounce = false end).

(Alternatively, you could figure out what's causing the event to fire more than once and try to stop that.)

6 comments

0

Ive never heard of delay before? Does that run everything below the code and wait on only that 1 line? Because I that is amazing if it does. Ive been wishing Studio had something like that forever Ive just never heard about it.

0

But also I added a wait after the print statement in my script and it still fires multiple times? Any ideas still?

0

May I add the first time I trigger the animation it never prints multiple times, but the second time running the animation it always triggers multiple times.

0

Like it does it in a pattern, it prints 1 time then 4 times then 1 time then 4 times. Its super weird and I dont know why but it always does it in that pattern.

0

Okay I tried out the delay part and it worked :) Yayyyy thanks man!

0

Glad you got it working. To let you know, 'delay' waits the specified amount of time before running the function you give it in a new coroutine. If you wanted to use 'wait', you'd need to put it right before the 'KillDebounce = false' line.

This discussion is preserved as it was. New answers, comments, and votes are closed.