So I'm making an elevator that teleports players that enter it. When it has 2, 3, or 4 players it teleports the players. When players enter it, it sticks there character names in a table. So I'm trying to make a for loop that searches for their character in workspace but it is not working.
Heres the code:
local function Teleport() --Teleports players to tracks local St1 = Points.StartingLine1 local St2 = Points.StartingLine2 local St3 = Points.StartingLine3 local St4 = Points.StartingLine4 for index, value in pairs(game.Workspace:GetChildren()) do local value = game.Workspace[index] if value.Name == PlayerArray[1] then value:FindFirstChild("HumanoidRootPart").CFrame = St1.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[2] then value:FindFirstChild("HumanoidRootPart").CFrame = St2.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[3] then value:FindFirstChild("HumanoidRootPart").CFrame = St3.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[4] then value:FindFirstChild("HumanoidRootPart").CFrame = St4.CFrame + Vector3.new(0,5,0) end end end
Any idea's?
like this
--Make a loop while true do CheckState(spelercountdata, Server.spelercount) wait(Time) CheckState(ServerBankData,Server.BankState) wait(Time) -- Funtion(Special ID, What {Store or Bank in my case}) CheckState(ServerStoreData,Server.StoreState) wait(Time) CheckState(HelperData, Server.Helper) wait(Time) CheckState(ServerRestart1Data,Server.Restart1) wait(Time) CheckState(ServerRestartData,Server.Restart) wait(TimeToWait) end
it repeats this all the time
I am not really sure what is wrong with this script, maybe you are not saving characters names properly? If I may make a suggestion though... What you really should do instead of searching the workspace for characters, you should use Players service. Try this solution:
local Players = game:GetService("Players") local function Teleport() --Teleports players to tracks local St1 = Points.StartingLine1 local St2 = Points.StartingLine2 local St3 = Points.StartingLine3 local St4 = Points.StartingLine4 for i, player in pairs(Players:GetPlayers()) do if player.Name == PlayerArray[1] then player.Character.HumanoidRootPart.CFrame = St1.CFrame + Vector3.new(0,5,0) elseif player.Name == PlayerArray[2] then player.Character.HumanoidRootPart.CFrame = St2.CFrame + Vector3.new(0,5,0) elseif player.Name == PlayerArray[3] then player.Character.HumanoidRootPart.CFrame = St3.CFrame + Vector3.new(0,5,0) elseif player.Name == PlayerArray[4] then player.Character.HumanoidRootPart.CFrame = St4.CFrame + Vector3.new(0,5,0) end end end
If it still does not work, please check names in PlayerArray table.
Well, I found the issue.
Using a for loop the way I did will require a table, I though game.Workspace:GetChildren() would do, but instead I just need to make a variable:
Ws = game.Workspace:GetChildren()
And Ws is now it's own independent table that's the children of workspace. So if we use Value = Ws[Index] rather than Value = game.Workspace[Index] it will give the desired result.
Final Code:
local function Teleport() --Teleports players to tracks local St1 = Points.StartingLine1 local St2 = Points.StartingLine2 local St3 = Points.StartingLine3 local St4 = Points.StartingLine4 local Ws = game.Workspace:GetChildren() for index, value in pairs(Ws) do local value = Ws[index] if value.Name == PlayerArray[1] then value:FindFirstChild("HumanoidRootPart").CFrame = St1.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[2] then value:FindFirstChild("HumanoidRootPart").CFrame = St2.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[3] then value:FindFirstChild("HumanoidRootPart").CFrame = St3.CFrame + Vector3.new(0,5,0) elseif value.Name == PlayerArray[4] then value:FindFirstChild("HumanoidRootPart").CFrame = St4.CFrame + Vector3.new(0,5,0) end end end