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

Run function only when the player touches the block?

Asked by 4 years ago
Edited 4 years ago

So I have a parkour game and I want to do a small Halloween event. The idea is when a player clicks the event button a pumpkin is placed on their head and their view is locked to 0. The player goes on the course and does his run. If the player touches the "Failed" block the pumpkin goes away and the main GUI becomes visible. Then he is able to zoom in and out. However, the problem with that is when a player jumps on a moving obstacle and the obstacle falls down, that part of the obstacle touches the "Failed" and it makes the Pumpkin invisible even though the player never failed. How can I make it so that it runs the Failed function only when PLAYER touches FAILED, not when any other BLOCK touches FAILED.

failed.Touched:connect(function()
    pumpk.Visible = false
    holder.Visible = true
    player.CameraMaxZoomDistance = 20
    player.CameraMinZoomDistance = 0
end)

1 answer

Log in to vote
2
Answered by
Optikk 499 Donator Moderation Voter
4 years ago

The problem is, the Touched event fires regardless of whether or not a part of a player touched it. You're going to want to check to make sure that a player touched the failed part and not something else.

The Touched event has a single argument called otherPart, as seen here. Here is an example of that in action:

failed.Touched:Connect(function(otherPart)
    print(otherPart.Name .. ' touched the failed part!')
end)

Go ahead and give this a try, so you can better understand what's going on.

I'm assuming you already have the player you want to work with assigned to player, so we're already almost at a solution. You can check if the otherPart is actually part of the player's character, and not just some stray part that set off the Touched event.

if otherPart.Parent == player.Character then
    print("i am part of my player's character!")
end

Putting it all together, you may have something like this:

failed.Touched:Connect(function(otherPart)
    if otherPart.Parent == player.Character then
        print("i am part of my player's character!")
        -- code for when player touches the part
    end
end)

It's simple as that. You should also consider disconnecting the Touched event when you no longer need it. Nothing will break if you don't, but it's a good idea to not listen to events when you don't need to.

Ad

Answer this question