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

Is debounce for this script working properly?

local debounce = false
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
function attack(hit)
	if hit and hit.Parent then
		local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
				debounce = true
				while debounce do
					toolNoning:Play()
					print "wait"
					wait(3)
					torso.TouchEnded:connect(function(hit2)
						if hit2 == hit then
							debounce = false
						end
					end)
				end
			end
		else
			if hit.CanCollide then
				humanoid.Jump = true
			end
		end
	end
end

Game tends to crash if many humanoid parts (such as a gun held by a humanoid with many parts) collide with it, but I'm fairly sure the debounce mechanism is supposed to stop that....

1 answer

✓ Accepted answer

Creating a connection in a while loop will repeatedly generate the connection and they will not go away. This will crash your game. The connection should be made before the while loop, and disconnected when it's finished.

local debounce = false
local debounce2 = false
local toolNoning = humanoid:LoadAnimation(toolNoneAnim)
function attack(hit)
	if hit and hit.Parent then
		local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			if enemy:IsA("Humanoid") and hit.Parent.Name ~= sp.Name then
				debounce = true
				local connection;
				connection=torso.TouchEnded:connect(function(hit2)
					if hit2 == hit then
						debounce = false
						connection:disconnect()
					end
				end)
				while debounce do
					toolNoning:Play()
					print "wait"
					wait(3)
				end
			end
		else
			if hit.CanCollide then
				humanoid.Jump = true
			end
		end
	end
end

3 comments

0

So call"torso.TouchEnded:connect(function(hit2)" outside the while loop?

0

btw, fan of your Laser Tanks game.

0

Thank you so much!

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