I'm creating a minigame-based game, and the main function is teleporting the players to the selected maps (from a server script in ServerScriptService). However, when testing it in multiplayer, I found out that it teleports one player and then teleports the next player when the round is over. Any help would be appreciated :).
01 | for i,player in ipairs (game.Players:GetChildren()) do |
02 | if player~ = nil then |
03 | if player.Character:FindFirstChild( "HumanoidRootPart" )~ = false then |
04 | print ( "its all relative!!!" ) |
05 | if player.Character.Humanoid.Health~ = 0 then |
06 | local ransp = mapSpawn [ math.random( 1 ,#mapSpawn) ] .CFrame*CFrame.Angles( 0 , 180 , 0 ) + Vector 3. new( 0 , 3 , 0 ) |
07 | print ( "ITS ALL RELATIVE" ) |
08 | player.Character.HumanoidRootPart.CFrame = ransp |
09 | wait() |
10 |
11 |
12 |
13 | end |
14 | end |
15 | end |
Improvements
Use :GetService()
for the Players
service
Use :GetPlayers()
instead of :GetChildren()
to retrieve players in the game
You can place multiple conditions in a if
statement with and
/ or
Use :SetPrimaryPartCFrame()
to move the player's Character
model instead of setting the CFrame
of a part of their body (as it may move only the part and not the whole model)
Issues
ipairs iterates through a table until it hits a nil value and then stops, use pairs instead.
Revised Server Script
1 | for i, player in pairs (game:GetService( "Players" ):GetPlayers()) do |
2 | local root = player.Character:FindFirstChild( "HumanoidRootPart" ) |
3 | local human = player.Character:WaitForChild( "Humanoid" ) |
4 | if root and human.Health > 0 then |
5 | local ransp = mapSpawn [ math.random( 1 , #mapSpawn) ] .CFrame * CFrame.Angles( 0 , 180 , 0 ) + Vector 3. new( 0 , 3 , 0 ) |
6 | player.Character:SetPrimaryPartCFrame(ransp) |
7 | wait() |
8 | end |
9 | end |