My script checks to see if the person is afk or not and teleports them, But it teleports everyone based on one persons decision how would I make the 'teleport' line only work for those whos afk values are true?
Spawns = game.Workspace.Spawns Players = game.Players:GetPlayers() --Define players [x2] for PlayerTeleport = 1, #Players do for _, Player in pairs(game.Players:GetChildren()) do if Player.AFK.Value == true then --Looks at value and sees if false or true Players[PlayerTeleport].Character.Torso.CFrame = Spawns[_G.StartingPlaces[math.random(1,#_G.StartingPlaces)]].CFrame * CFrame.new(0,8,0) --teleports players end end end
The reason for the error is because you've got two for loops in your script, looping through each player in the game. In your script, you're basically telling the computer to do is to teleport all the players if a player's AFK value is true.
To fix this, you only need to get rid of one of the for loops and either one of the two ends at the bottom of your script, depending on which for loop you remove. You will then need to modify your script appropriately so that it will fit the for loop you've chosen to keep.
If you choose to remove the first for loop surrounding the other for loop, your script will look like this:
Spawns = game.Workspace:WaitForChild("Spawns") --Waits for the Spawns to become available. --No need for the players variable, we're not using a numerical for loop. for _, Player in pairs(game.Players:GetPlayers()) do--Using GetPlayers to eliminate the chance of a non-player object being detected by the script. if Player:WaitForChild("AFK").Value == true then --Waits for the AFK value and then checks if it's true. local char = Player.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
If you choose to remove the second for loop, your script will look like this:
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
I hope my answer helped you. If it did, be sure to accept it.