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

How would I go about checking if every player is touching a part, and if they are then execute code?

Asked by 2 years ago

I've exhausted about every bit of my knowledge with Touched and GetTouchingParts(), so I was looking for some advice. I'm trying to make an elevator game and once the level is complete, the door cannot be closed until every player is inside.

I know I should do something with an invisible part and the Touched thing, but I can't think exactly what I should do so that my code is efficient.

What I was hoping for would be sort of like this (after all the necessary setup of course)

if #PlayersTouchingPart == #Players:GetPlayers() then
    CloseDoor() --function for closing door
    return
else
    return
end

Although I've tried this and I just can't get around it. Any ideas or advice would be appreciated.

Merry Christmas / Happy Holidays!

0
btw this would already be inside another function for the button being pressed and checking the door position and what not hoppyman360 -13 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

I think your best bet would be to create a table of players that are in the elevator. This could be done by getting the characters that are touching the elevator floor and using Players:GetPlayerFromCharacter()

local PlayersInElevator = {}
for i,v in pairs(Part:GetTouchingParts()) do
    local char = v.Parent
    if char:FindFirstChild("Humanoid") then
        local plr = Players:GetPlayerFromCharacter(char)
        if plr then
            table.insert(PlayersInElevator, plr)
        end
    end
end

With this table of players, you could loop through the list of Players that are in the server, and check if each of them are in the table. Maybe check every half second or so

local function CheckElevatorPlayers()
    for i,plr in pairs(Players:GetPlayers()) do
        if table.find(PlayersInElevator, plr) == nil
            return false --there is a player that isn't in the elevator
        end
    end
    return true
end

And if this function returns true, you know that its safe to use CloseDoor().

Ad
Log in to vote
0
Answered by 2 years ago

It doesn't seem to work. I'll try to provide a better section of code.

Click.MouseClick:Connect(function()
    if IsLevelLoaded.Value == true then
        if Door.Position == Vector3.new(21.5, 7, -21.5) then
            --here would be the check for if every player is in the elevator, (and of course if         the level has been completed, which I've already figured out)
            MoveDoor(Vector3.new(21.5, 7, -26.5))
            Door["close"]:Play()
            script.Parent.Position = Vector3.new(24.5, 6.5, -20.5)
            return
        end
        if Door.Position == Vector3.new(21.5, 7, -26.5) then
            MoveDoor(Vector3.new(21.5, 7, -21.5))
            Door["open"]:Play()
            script.Parent.Position = Vector3.new(24.5, 5.5, -20.5)
            return
        end
        print("door is currently in motion")
        return
    end
    print("level is not loaded yet")
end)

Answer this question