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

Function isn't connecting to OnTouched when the same part touches it the second time?

Here is my code:

local debounce = false

local function Hurt(Part)
	if debounce == false then
		print "check1"
		if Part.Name == "box" then
			print "check2"
			Part.Position = Vector3.new(0, 3, 0)
			debounce = true
			game.Workspace.Baseplate.BrickColor = BrickColor.new("Institutional white")
			wait(0.5)
			game.Workspace.Baseplate.BrickColor = BrickColor.new("Pearl")
			debounce = false
		end
	end
end
script.Parent.Touched:connect(Hurt)

There is a box that the player pushes to touch borders around a specific area. This is the script in the borders. When the box is pushed and touches the border, it works the first time it is pushed to the border, but doesn't work when the box is pushed to the same border again. Any suggestions?

7 comments

0

yes yes yes yes yes

0

bruh

0

You have a couple things in the script that are unconventional, for example 'game.Workspace' instead of 'workspace', 'print "check1"' instead of 'print("check1")', and 'if debounce == false' instead of 'if not debounce'. Try using the above suggestions

0

As a suggestion maybe put 'print(Part.Name)' under 'print "Check1"'

0

Alright,

1

Turns out that this is a collision glitch on roblox. Sorry for wasting your time.

1

It's ok. Please make a new answer saying "It was just a glitch" and then accept it

1 answer

Debounce. That is pretty much all I have to say.

So your script might be firing but not finding the debounce. To fix this:

if debounce == false then
     debounce = true
     print("check1")
end
wait(0.5)
debounce = false

Note that you have the reset in your script, you'll need to move it and anything after. Also, you'll want to change the deboucne to true before anything happens with the script. This will make sure that the function won't fire several times before the part is even found.

However, the script might not be firing at all. To check:

local function Hurt(Part)
     print("Function ran")
end

Theoretically, this should answer your question. If it doesn't please tell me and I'll hop on Studio ASAP to fix it.

EDIT:

So your script isn't detecting the Part's name. You can add

else
     print(Part.Name)
end

to your if statement to check what the function is finding. Something you can try is to look at the hitting part's parent and find "box":

if Part.Parent then
     if Part.Parent:FindFirstChild("box") then
     end
end

These 2 if statements would replace your current if part.Name statement.

I hope this now answers your question

2 comments

0

When the box touches the part, it prints "check1" but not "check2" when touched the second time.

0

Therefore, I doubt that debounce is the issue.

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