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

How can you tell when something STOPS touching another object?

Asked by 9 years ago

For example, if I wanted to tell how long that a particular player was inside an object, how would I tell when they went out of contact with that object?

All support is appreciated, thanks!

2 answers

Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
9 years ago

Adding on to Goulstem's response, that would not work as expected if another part touches and stops touching the part, it will act as if the original part has stopped touching it. This is what I would do.

db = false

script.Parent.Touched:connect(function(hit)
    if db == false then db = true end
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        before = tick()
        local c
        c = script.Parent.TouchEnded:connect(function(hit2)
            if hit2 == hit then
                tm = math.floor(tick() - before)
                print("Player was touching part for "..tm.." seconds!")
                c:disconnect()
            end
        end)
    end
    db = false
end)
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

The TouchEnded Event does that.

So to do what you want;

db = false

script.Parent.Touched:connect(function(hit)
    if db == false then db = true end
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        before = tick()
        script.Parent.TouchEnded:wait()
        after = tick()
        tm = math.floor(after - before)
        print("Player was touching part for "..tm.." seconds!")
    end
    db = false
end)

-Goulstem

0
@Ekkoh, the debounce prevents that. Goulstem 8144 — 9y

Answer this question