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

What can I add to the script so the system only sends a minimum of 1 person for each teleport block?

Asked by 9 years ago
local gameSpawn = game.Workspace.gameSpawn
local lobbySpawn = game.Workspace.Lobby.lobbySpawn.lobbySpawn
local minimumGameSpawn = 1
local minimumLobbySpawn = 4

function teleportPlayers(target)
    for _, player in pairs(game.Players:GetChildren()) do
        local character = player.Character
        local torso = character.Torso
        torso.CFrame = target.CFrame

    end
end 




wait(5)

print("Racers To Your Karts")

wait(2)

teleportPlayers(gameSpawn)

Also its fine when i test this by myself in solo mode or play mode but when i test this in test mode from the clients and servers area to where you can test with multiple people you can control with different windows, IT DOESNT teleport them. Please help

1 answer

Log in to vote
-1
Answered by 9 years ago

For the teleporting players, you're basically passing a table as an argument to the function. Here's how I'd set it up:

local targets = {} --Either a table or a model of the parts you want to teleport players to. If it's a model, then define it as model:GetChildren()

local function teleport(targets)

local targets = targets --Defines targets as local so that manipulating the table in the loop doesn't effect the one outside the function

for i,v in ipairs(game.Players:GetChildren()) do --ipairs is a lot like pairs, except it stops iterating if it finds a nil value in the table

local ran = math.random(1,#targets) --returns a random number bewteen 1 and the length of the table

pcall(function() v.Character.HumanoidRootPart.CFrame = targets[ran].CFrame end) --pcall runs a function in safemode, so if it results in an error, it keeps running the following code. It also returns a bool and an error message. I used an anonymous function in it, so that I don't have to redefine one every time we call the function in the loop.

table.remove(targets,ran)--removes the target from targets to prevent players from being teleported to the same target

end--end loop
end--end function

for i = 20,0,-1 do --countdown to give players time to join, optional.
wait(1)
print(i)
end

teleport(targets)--calls the function
0
Please explain ipairs and pcall sorry im a bit of an amateur to programming but i study everyday robloxiveboy 25 — 9y
0
theres also an error in the equal sign im tryign to fix it but idk whats causign the error robloxiveboy 25 — 9y
0
Just hopped on my computer, gonna edit my answer soon aquathorn321 858 — 9y
0
Edited and tested my answer, works just fine. aquathorn321 858 — 9y
Ad

Answer this question