This script checks to see if the palyer's afk value is true or not and teleports them if true, except it breaks the script if it's true and has no error output
Spawns = game.Workspace:WaitForChild("Spawns") --Waits for the Spawns to become available. Players = game.Players:GetPlayers() --Define players for PlayerTeleport = 1, #Players do if Players[PlayerTeleport]:WaitForChild("AFK").Value == true then --Waits for the AFK value and then checks if it's true. local char = Players[PlayerTeleport].CharacterAdded:wait() --Essentially waits for the CharacterAdded event to fire, then returns the character. char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) --Waits for the torso to become available before teleporting. end end
Your only error here was when the character is already added you can't does like you do:
local char = Players[PlayerTeleport].CharacterAdded:wait()
Your code with error here:
Spawns = game.Workspace:WaitForChild("Spawns") Players = game.Players:GetPlayers() for PlayerTeleport = 1, #Players do if Players[PlayerTeleport]:WaitForChild("AFK").Value == true then local char = Players[PlayerTeleport].CharacterAdded:wait() -- the error is here. char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) end end
change it to:
Spawns = game.Workspace:WaitForChild("Spawns") Players = game.Players:GetPlayers() for PlayerTeleport = 1, #Players do if Players[PlayerTeleport]:WaitForChild("AFK").Value == true then local char = Players[PlayerTeleport].Character char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) end end
You can also do it for make it more easy to read:
Spawns = game.Workspace:WaitForChild("Spawns") for _, PlayerTeleport in pairs (game.Players:GetChildren()) do if PlayerTeleport:WaitForChild("AFK").Value then local char = PlayerTeleport.Character char:WaitForChild("Torso").CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) end end