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

Script Debounce determination problem, help?

Alright, so I have a script that when a part named "Bullet" touches a block it is supposed to add a different number to the block rather than what a normal touch is supposed to do, however when "Bullet" comes in contact with the block it gives the number as any normal touch. What do I do?

function Bullet(part)
    if debounce or part.Name == Bullet then return end
	onTouched()
    debounce = true
	number1 = tonumber(script.Parent.Name)
    print(number1+number3) 
    script.Parent.Name = (number1+number3)
    wait(0.2)
    print "bullet"
    math()
	math2()
	math3()
	math4()
    debounce = false
end

script.Parent.Touched:connect(Bullet)

function onTouched()
    if debounce == true then return end
    debounce = true
    number1 = tonumber(script.Parent.Name)
    print(number1+number2) 
    script.Parent.Name = (number1+number2)
    wait(0.2)
    print "ahh"
	Bullet(part)
    math()
	math2()
	math3()
	math4()
    debounce = false
end

NOTE: The full code leaves me with no errors, and in practice it also leaves no errors.

1 answer

✓ Accepted answer

You missed quotes around Bullet.

Bullet is a variable; "Bullet" is a string; part.Name is also a string. The types need to match for them to be equal.

That said, your organization is also a mess - your two functions are calling each other, which, if it weren't for the debounce, would cause an infinite loop.

Instead, do this:

function Bullet(part)
	--what you want to do when a bullet hits the brick
end
function NormalTouch(part)
	--what you want to do when a non-bullet hits the brick
end
script.Parent.Touched:connect(function(part)
	if part.Name == "Bullet" then
		Bullet(part)
	else
		NormalTouch(part)
	end
end)

debounce is only required if your scripts need to wait (or call "yielding" functions, like :WaitForChild, or use RemoteFunctions, etc)

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