Why isnt the for loop iterating

Trying to add int value to all the the players but its not iterating

my code looks right but idk what I’m doing wrong

heres my code and its in server script service

local playerService = game:GetService("Players")
local players = playerService:GetPlayers()

for _, player in pairs(players) do
	local int = Instance.new("IntValue")
	int.Name = "playersLeft"
	int.Value = false
	int.Parent = player
	print("playersFound") -- this isnt printing
end
1 Like

Is this for loop running before any player instances are connected to the game server

1 Like

Its because there are no players on the server when the code is ran.
you would be better off using the PlayerAdded event.

local playerService = game:GetService("Players")
playerService.PlayerAdded:Connect(function(player)
    local int = Instance.new("IntValue")
	int.Name = "playersLeft"
	int.Value = false
	int.Parent = player
end

this will run for every player that joins the game.

1 Like

Close, just remember that this is an IntValue, so you can only assign numbers to it, not booleans. You also need a folder, (leaderstats if you want it to show) to store the stats in!

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")  
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

    local int = Instance.new("IntValue")
	int.Name = "playersLeft"
	int.Value = 0
	int.Parent = leaderstats
end

If you want to store booleans, maybe try replacing IntValue with BoolValue

2 Likes

i forgot to change that as it was going to be a bool value but i forgot about that