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

How do I ensure my TouchEnded event is only triggered legitimately?

Asked by 5 years ago

I have the following code:

local ids = {}

local function hasValue(tab, value)
    local contains = nil
    for k,v in pairs(tab) do
        if v == value then
            contains = k
            break;
        end
    end
    return contains
end

script.Parent.Detector.Touched:connect(function(obj)
    if obj.Name == "HumanoidRootPart" then
        local player = game.Players:GetPlayerFromCharacter(obj.Parent)
        if player then
            if hasValue(ids, player.UserId) == nil then
                print(player.UserId, "is here")
                table.insert(ids, player.UserId)
            end
        end
    end
end)

script.Parent.Detector.TouchEnded:connect(function(obj)
    if obj.Name == "HumanoidRootPart" then
        local player = game.Players:GetPlayerFromCharacter(obj.Parent)
        if player then
            local k = hasValue(ids, player.UserId)
            if k ~= nil then
                print(player.UserId, "left")
                ids[k] = nil
            end
        end
    end
end)

It all works fine and dandy, the players UserId stays in the table until they leave the part... At least while they are in third person. As soon as they are in first person, and move or turn even the smallest amount, they constantly trigger the TouchEnded and Touched event. This ruins what I'm using this for, and so I need a way around it. How would I go about fixing this?

0
If you want to detect players inside a part or above then you should try Region3. Touched event seems to be inconsistent with how it treats CanCollide objects. xPolarium 1388 — 5y
0
The object has collision disabled, but I'll check out Region3. FrostTaco 90 — 5y
0
Alright, so I got it working using Region3, but due to the fact that I only want ONE of each player in my table, I have to keep using that same hasValue function to see if the player is already in my table, which seems pretty inefficient to me.a FrostTaco 90 — 5y

Answer this question