Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
DemGame 271 Moderation Voter
3 years ago
Edited 3 years ago

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?

0
yes yes yes yes yes rocknrollLuxor -6 — 3y
0
bruh DemGame 271 — 3y
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 User#30567 0 — 3y
0
As a suggestion maybe put 'print(Part.Name)' under 'print "Check1"' User#30567 0 — 3y
View all comments (3 more)
0
Alright, DemGame 271 — 3y
1
Turns out that this is a collision glitch on roblox. Sorry for wasting your time. DemGame 271 — 3y
1
It's ok. Please make a new answer saying "It was just a glitch" and then accept it User#30567 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

0
When the box touches the part, it prints "check1" but not "check2" when touched the second time. DemGame 271 — 3y
0
Therefore, I doubt that debounce is the issue. DemGame 271 — 3y
Ad

Answer this question