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 :).
for i,player in ipairs(game.Players:GetChildren()) do if player~=nil then if player.Character:FindFirstChild("HumanoidRootPart")~=false then print("its all relative!!!") if player.Character.Humanoid.Health~=0 then local ransp = mapSpawn[math.random(1,#mapSpawn)].CFrame*CFrame.Angles(0,180,0) + Vector3.new(0,3,0) print("ITS ALL RELATIVE") player.Character.HumanoidRootPart.CFrame=ransp wait() end end 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
for i, player in pairs(game:GetService("Players"):GetPlayers()) do local root = player.Character:FindFirstChild("HumanoidRootPart") local human = player.Character:WaitForChild("Humanoid") if root and human.Health > 0 then local ransp = mapSpawn[math.random(1, #mapSpawn)].CFrame * CFrame.Angles(0, 180, 0) + Vector3.new(0, 3, 0) player.Character:SetPrimaryPartCFrame(ransp) wait() end end