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

Make a script that teleports all players to a baseplate after there are 2 players in the game?

Asked by 6 years ago

I am trying to create a code that teleports all players to the baseplate from the lobby once there are at least two players in the game. If anyone could help it would be much appreciated.

1 answer

Log in to vote
0
Answered by 6 years ago

This isn't a request site.

I'll try to help you though (just create a script next time!)

So first you would need to count the players in the game.

You can easily do this by counting the children of players

1) Get the children

2) Get the amount of them

Like this

while wait() do
    local plrs = #game:GetService('Players'):GetChildren()
end

The # is to get the amount of them

You test the am of players using if.

To teleport the player you simply change the HRP CFrame

Like this

while wait() do
    local plrs = #game:GetService('Players'):GetChildren()

    if plrs >= 2 then

    end
end

To get all players use for i,v in pairs

Like this

while wait() do
    local plrs = #game:GetService('Players'):GetChildren()

    if plrs >= 2 then
        for index, plr in pairs(game.Players:GetPlayers()) do
            local char = plr.Character or plr.CharacterAdded:wait() -- Get the character of the player

            local HRP = char:WaitForChild('HumanoidRootPart') -- Wait For the HumanoidRootPart
            HRP.CFrame = CFrame.new(x,y,z) -- CHange the XYZ with the position or use script.Parent.CFrame for example
        end
    end
end

Finally you want to add a debounce to not keep teleporting them

Debounce = true

while wait() do
    local plrs = #game:GetService('Players'):GetChildren()

    if plrs >= 2 and Debounce then
        Debounce = false
        for index, plr in pairs(game.Players:GetPlayers()) do
            local char = plr.Character or plr.CharacterAdded:wait() -- Get the character of the player

            local HRP = char:WaitForChild('HumanoidRootPart') -- Wait For the HumanoidRootPart
            HRP.CFrame = CFrame.new(x,y,z) -- CHange the XYZ with the position or use script.Parent.CFrame for example
        end
    end
end

You can run the script again by setting the Debounce to true again

If you have a question, just ask :)

0
Why are you helping this dude. You dont just give free answers away Axceed_Xlr 380 — 6y
0
Why would you use a while loop to check if there are 2 players??? This is what events are for (Players.PlayerAdded) Amiaa16 3227 — 6y
0
Same thoughts as @Kiriot22 , use PlayerAdded and if you really must do it this way, give the script more time than a pure "wait()" --> 0.03 seconds. You dont need to check 30 times every second, this will be really laggy. fighter169mobile 123 — 6y
0
Hey buddy, @Axceed_Xl, this is literally a site where people help others with scripting. Why would he waste his time giving a vague answer when he could actually just directly help the person? coolduck12 4 — 6y
0
Well, to answer you guys why I use a loop is because when you want to start again you won't be able too with a playeradded event, you could use it, but it could easily break and isn't that correct as a loop, you can add a 1 to the wait though to make it less laggy as it doesn't need to update every second User#20388 0 — 6y
Ad

Answer this question