Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Do command for single person, not everyone help????

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

2 comments

0

You already have a check on line 8...

0

Yeah, I have the check but the function for teleporting works on everyone, not just that player

1 answer

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.

1 comment

0

Updated my script in case it doesn't work.

This discussion is preserved as it was. New answers, comments, and votes are closed.