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

I keep getting an error that doesn't make sense to me, How can I fix it?

Asked by 9 years ago
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 '='

2 answers

Log in to vote
0
Answered by 9 years ago
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.

0
pcall has a lot of useful functionality to it, in my opinion. It makes scripts a lot safer. If you had a large script without pcall, and it happened to come across an error in-game, then it would end up breaking the script. You can also use it for bug testing. aquathorn321 858 — 9y
0
Whats i pairs i still dont understand robloxiveboy 25 — 9y
0
16:04:44.772 - ServerScriptService.Script:8: bad argument #2 to 'random' (interval is empty) robloxiveboy 25 — 9y
0
nvm i think i foudn the problem i forgot to defien variable but still i dont understad how you want me to define target. The model i want to tp to is called gameSpawn robloxiveboy 25 — 9y
Ad
Log in to vote
0
Answered by
DataStore 530 Moderation Voter
9 years ago

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) 
0
k thanks this pcall was from someone else they told me instead of using a different teleport function use this one and i tried it. robloxiveboy 25 — 9y

Answer this question