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

How do I get the number of players touching a part? (Touched event)

Asked by 3 years ago
Edited 3 years ago

I trying to make a part if there are 3 or more people walk on that. they'll die, but i dont know why is it not working.

    local Part = script.Parent
    Part.Touched:Connect(function(hit)
for i,v in pairs(#hit.Parent.Name:GetChildren()) do --- Need fix
                            if i > 3 then
                                print("Zomb")
                                Zomb()
                                else break
                            end
end)

2 answers

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
3 years ago
Edited 3 years ago

You could try this but I don't know if it works because I didn't try it

local part = script.Parent -- The Part

local Touching = {  } -- Everyone who touches the part

part.Touched:Connect(function(hit) -- Fires when someone touches it
    local char = hit.Parent -- Gets the character
    if game.Players:GetPlayerFromCharacter(char) then -- Checks to see if it is actually a player
        table.insert(Touching, game.Players:GetPlayerFromCharacter(char)) -- Adds them to the touching table
    end
    if #Touching >= 3 then -- if there is 3 or more people touching
        for i,v in pairs(Touching) do
            v.Character.Humanoid.Health = 0 -- kills
            table.remove(Touching, i) -- removes player from the table
        end

    end
end)

part.TouchEnded:Connect(function(hit)
    local char = hit.Parent -- Gets the character
    if game.Players:GetPlayerFromCharacter(char) then -- Checks to see if it is actually a player
        for i,v in pairs(Touching) do
            if v.Name == game.Players:GetPlayerFromCharacter(char).Name then -- finds where player is located in the table
                table.remove(Touching, i) -- removes player from table
            end
        end
    end
end)
0
your idea works. I just need to adjust something. Thank you! thinhs11 -2 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You can try making a number value and then in your script put:

local part = script.Parent

part.Touched:Connect(function(hit)
    local PlrsTouchingCount = script.Parent.NumberValue --I said NumberValue but make sure to change this into the name of your number value
    PlrsTouchingCount.Value = PlrsTouchingCount.Value+1
end)

--Now we are gonna make it so when they are not touching the part then it doesnt count them in the NumberValue

part.TouchEnded:Connect(function(hit)
    local PlrsTouchingCount = script.Parent.NumberValue --Remember change the word NumberValue into the name of your real NumberValue
    PlrsTouchingCount.Value = PlrsTouchingCount.Value-1
end)

Hope this helps :D

0
wait i just noticed you wanted it at 3! Oop lol MarcTheRubixQb 153 — 3y
0
Sorry about that i'm fixing the script rn MarcTheRubixQb 153 — 3y
0
I want it to be like if there is a 3rd person touch the part, then 3 of them will die thinhs11 -2 — 3y

Answer this question