Detect if a player is touching a part

Any suggestions on how to detect if a player is currently touching a part? I’ve seen people do this many times before and I am curious on how this is done? Please note, I’m not trying to detect when a player TOUCHES a part, but if they’re CURRENTLY touching a part.

1 Like

Since there’s a .Touched and .TouchEnded event, you just listen for the .Touched event and execute what you want to do until .TouchEnded gets fired.

It’s wrong, the post saying that it supposed to meant for Region3 or GetPartsBoundsInBox.

I don’t get your point here, could you elaborate on that please?
While not the most effective, my idea would work.

2 Likes

I tried using Touched and TouchEnded events one of my games to make music, but it bugged out and it plays two musics at the same time, so thats why we use Region3 or GetPartsBoundsInBox

This was my old system but sometimes while the player is touching the part the script would think the the player wasn’t which is why I need a new system.

local part = script.Parent

local hits = {}

part.Touched:Connect(function(hit)
    local character = hit:FindFirstAncestorOfClass("Model")

    if character then
        local player = game.Players:GetPlayerFromCharacter(character)

        if player and not table.find(hits, player) then
           table.insert(hits, player)
           print("hit")
        end
    end
end)

part.TouchEnded:Connect(function(hit)
    local character = hit:FindFirstAncestorOfClass("Model")

    if character then
        local player = game.Players:GetPlayerFromCharacter(character)

        if player and table.find(hits, player) then
           table.remove(hits, table.find(hits, player)
        end
    end
end)

or

local RunService = game:GetService("RunService")

local part = script.Parent

RunService.Heartbeat:Connect(function()
    local parts = part:GetTouchingParts()

    for i,v in ipairs(parts) do
        local character = v:FindFirstAncestorOfClass("Model")

        if character then
            local player = game.Players:GetPlayerFromCharacter(character)

            if player then
                print("hit")
            end
        end
    end
end)

I thought something like this might work but I wanted to see if there were any other alternatives first. I’ll try this out though, will get back to you later.

1 Like