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

How to accurately detect when a player fully stops touching a part? [SOLVED]

Asked by 6 years ago
Edited 6 years ago
had to trim the title quite a bit

edit: where are all the comments and answers why do i have only one dude who has commented and answered this

local debounce = false
script.Parent.Touched:Connect(function(otherPart)
    local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
    if player and not debounce then
        debounce = true
        game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
        script.Parent.TouchEnded:Wait()
        wait(1)
        debounce = false
    end
end)

hold up

what's that I see?

huhr?

i can't read it

lets zoom in

script.Parent.TouchEnded:Wait()

ok let's be serious now

You'd think, "ey this'll work perfectly"

Well, until you pay attention to this:

players have multiple parts

Soo...

What's the best way to get when the player actually stops touching the part?

only revert to player:GetDistanceFromCharacter(script.Parent.Position) if it's the only way out
and note: plz make sure u include how 2 do it with R15

EDIT: I figured it out, you just have to use :GetTouchingParts() and check if the parents of any of the given values are the player's Character. I will keep this question hanging around so that others can see.

local debounce = true
script.Parent.Touched:Connect(function(otherPart)
    local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
    if player and debounce then
        debounce = false
        game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
        local function waitUntilNoMoreCollision() -- importent
            while true do
                local s = false -- importent
                for i,v in pairs(script.Parent:GetTouchingParts()) do -- importent
                    if v.Parent == player.Character then -- importent
                        s = true -- importent
                    end
                end
                if not s then -- importent
                    print("Player stopped touching!")
                    return -- importent
                end
                wait() -- ultra importent
            end
        end
        waitUntilNoMoreCollision() -- importent
        debounce = true
    end
end)

2 answers

Log in to vote
0
Answered by
sad_eyez 162
6 years ago

I would just do it like this which works perfect everytime.

Code:

script.Parent.TouchEnded:Connect(function(hit)

    if (hit.Parent:IsA("Model")) and (hit.Parent:FindFirstChild("Humanoid")) then 

        -- Code

    end

end)

This basically checks if the parent of the part it hits is a model, and checks if it's a character by finding the humanoid.

0
Err... no. Won't make a difference, for as I mentioned in the question: PLAYERS. HAVE. MULTIPLE. PARTS. sweetkid01 176 — 6y
0
and dose parts be MOVING sweetkid01 176 — 6y
0
When you say multiple parts what do you mean, because R15 uses multiple parts already, and that works perfect for me sad_eyez 162 — 6y
0
When you say multiple parts what do you mean, because R15 uses multiple parts already, and that works perfect for me sad_eyez 162 — 6y
0
err... the parts go back and forth and it easily triggers the TouchEnded event with one part and then another part re-triggers the Touched event sweetkid01 176 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
local debounce = true
script.Parent.Touched:Connect(function(otherPart)
    local player = game:GetService("Players"):GetPlayerFromCharacter(otherPart.Parent)
    if player and debounce then
        debounce = false
        game:GetService("ReplicatedStorage").TouchEvent:FireClient(player, script.Parent)
        local function waitUntilNoMoreCollision() -- importent
            while true do
                local s = false -- importent
                for i,v in pairs(script.Parent:GetTouchingParts()) do -- importent
                    if v.Parent == player.Character then -- importent
                        s = true -- importent
                    end
                end
                if not s then -- importent
                    print("Player stopped touching!")
                    return -- importent
                end
                wait() -- ultra importent
            end
        end
        waitUntilNoMoreCollision() -- importent
        debounce = true
    end
end)

yey i am smort and i figured it out mahself

Answer this question