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

How do I check if a player is not touching a brick?

Asked by 8 years ago

Does anyone know how to check if a player is not touching a brick? I'm not sure how to do this.

1 answer

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

You have to use Touch and TouchEnded (you keep track of which players are touching the specified brick; if a player is not in that list, they are not touching it), unless you're willing to test for player proximity (ie test to see if a player is within some range of the target object). In testing, I discovered that TouchEnded doesn't always fire after Touched, nor will it always fire properly for HumanoidRootPart. Here's a "prototype" you can test out on your own:

parts = {}
workspace.SpawnLocation.Touched:connect(function(p) parts[p] = true end)
workspace.SpawnLocation.TouchEnded:connect(function(p) wait() parts[p] = nil end)
while wait(1) do
    print("----")
    for k, _ in pairs(parts) do
        print(k)
    end
end

You'll notice that if you touch the SpawnLocation and then stop touching it, HumanoidRootPart will usually still be being printed out.

The wait() in the 3rd line solves the problem where TouchEnded fires before Touched does. A proper function should keep track for each player and ignore HumanoidRootPart. I don't know if TouchEnded fires when a player leaves the game, so that should be considered also.

Ad

Answer this question