While loop only using newest player

In the following script, the code in the for loop is only running for the most recent user to join. I tried debugging it, but I still couldn’t find a source of the problem. I think it is in this function though, since this is where all the functions get called (and none of them got called).

while task.wait() do
		local players = game.Players:GetPlayers()
		for index, player in pairs(players) do
			if player.Character then
				local target = player.Character
				local root = target:FindFirstChild("HumanoidRootPart")
				if root then
					-- insert irrelevant code here
				end
			end
		end
	end

thanks for any help

I just tested your code in a test environment and it worked just fine. Could you be more specific on how you were testing and what the output looked like?

For my test environment, I did it with three players and all three got identified in the loop when I executed the below code:

while task.wait() do
	local players = game.Players:GetPlayers()
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local root = target:FindFirstChild("HumanoidRootPart")
			if root then
				print(player.Name)
			end
		end
	end
end

I didn’t tested it.
I made so random players get choosen.
Fixed Code:

while task.wait() do
		local players = game.Players:GetPlayers()
		for i = 1,#game.Players:GetPlayers() do
           local number = math.random(i,#game.Players:GetPlayers()
            local player = game.Players:GetPlayers()[number]
            table.remove(players,number)
			if player.Character then
				local target = player.Character
				local root = target:FindFirstChild("HumanoidRootPart")
				if root then
					-- insert irrelevant code here
				end
			end
		end
	end

I am currently making an automatic door. But it only activates with the newest player coming close. Here is the rest of the code. The error may be there.

When I use print(player.Name) it gives me the name of every single player but the door seems to be only reacting to the newest one.

while task.wait() do
		local players = game.Players:GetPlayers()
		for index, player in pairs(players) do
			if player.Character then
				local target = player.Character
				local root = target:FindFirstChild("HumanoidRootPart")
				if root then
					local distance = (PrimaryPart.Position - root.Position).Magnitude 

					local radius = 10

					if distance <= radius then -- If player is close
						Open = true
					else
						Open = false
					end

					if Open ~= LastOpen then -- LastOpen =  last state of the variable Open
						if Open == true then
							open(PrimaryPart.Parent.GlassDoor1.PrimaryPart, "L") -- Open door animations
							open(PrimaryPart.Parent.GlassDoor2.PrimaryPart, "R") -- Open door animations
						else
							close(PrimaryPart.Parent.GlassDoor1.PrimaryPart, "L") -- Close door animations
							close(PrimaryPart.Parent.GlassDoor2.PrimaryPart, "R") -- Close door animations
						end
					end

					LastOpen = Open -- setting LastOpen to current Open state
				end
			end
		end
	end

It is because

if distance <= radius then -- If player is close
	Open = true
else
	Open = false
end

repeats for EVERY player in the GetPlayers() table, and i assume the newest player is at the last index of that table, so if the newest player is not near the door, it will close, no matter if any other player is near it. A fix for this would be breaking out of the for loop if a character is found close to the door
after line

LastOpen = Open -- setting LastOpen to current Open state
if Open then
    break
end

this should make it so that the door will open if theres anyone found close to it, and it should close still if no one is near

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.