local gameSpawn = game.Workspace.gameSpawn local lobbySpawn = game.Workspace.Lobby.lobbySpawn.lobbySpawn local minimumGameSpawn = 1 local minimumLobbySpawn = 4 local function teleportPlayers(target) for i,v in ipairs(game.Players:GetChildren()) do pcall (v.character.HumanoidRootPart.CFrame = target.CFrame) end end wait(5) print("Racers To Your Karts") wait(2) teleportPlayers(gameSpawn)
I have an error at the equal sign at
pcall (v.character.HumanoidRootPart.CFrame = target.CFrame)"
The error states :
Expected ')' (to close "(" at column 8), got '='
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
That's the code you want to use if you want to teleport players to multiple places as you had earlier.
If you've coded something correctly you should never have to use pcall, seeing as it's a "lazy way out".
The issue with your pcall line is the fact that the pcall function takes a function as its first argument, followed by any arguments you wish to pass to that function. So, in order to "fix" line eight all you have to do is change it to the following:
pcall(function() v.Character.HumanoidRootPart.CFrame = target.CFrame end)