'for' loop won't print

Hey there all! So I was learning some random stuff when I came across a problem. The for loop won’t print anything. Just nothing. It’d be great if anybody could help, thanks!

local players = {}

for i, v in pairs(game.Players:GetPlayers()) do
	table.insert(players, {Player = v, Kills = v:WaitForChild("Kills")})
end

for _, v in pairs(players) do
	v["Kills"].Changed:Connect(function()
		print(v["Player"].Name)
		print(v["Kills"].Value)
	end)
end

I may be completely wrong here, but what happens if you replace

table.insert(players, {Player = v, Kills = v:WaitForChild("Kills")})

with

table.insert(players, {["Player"] = v, ["Kills"] = v:WaitForChild("Kills")})
1 Like

I feel like that’s the same thing, but I’ll give it a try.

Edit: Doesn’t work.

Hmm ok.
Can you try removing the v["Kills"].Changed:Connect(function() bit? (also remove the end) that goes with it), so that we can see if the table and printing actually works, and if it is just the changing of the value that doesn’t work

Still doesn’t print anything at all.

Ok, before

for _, v in pairs(players) do
	v["Kills"].Changed:Connect(function()
		print(v["Player"].Name)
		print(v["Kills"].Value)
	end)
end

can you please put

print(players)

It prints out table: 0x662da80ac598f123

can you please run it in studio and have a look in the console output (the one that is part of studio, not the one accessed by F9)

It prints it out through the studio output.

can you send me a screenshot of the menu where it says it?

Edit I might not be back in an hour.

huh, I usually get a different result when printing tables.
Can you, instead of having print(players) do

for i, v in pairs(players) do
    print(v)
end

game.Players:GetPlayers() is not a dictionary, so instead of using
in pairs you should use in ipairs:

for i, v in ipairs(game.Players:GetPlayers()) do

end

Just this to every loop and it should work.

in pairs also works in this instance.

Yep, it works fine, sorry. I’ve tested the code and it runs just fine, it prints the values once something changes. Maybe he changes the Kills value from the Client? Which does not print anything, if the script is server-sided.

When I told him to remove the v[“kills”].Changed bit, he said it still didn’t run

Hmm, then the error is that the player isn’t in the player’s list when the script is running. If the Script is in ServerScriptService or Workspace, it runs instantly, when no players are available.
There should be a connection when the player joins the server, and connect the Changed event:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
     local Kills = Instance.new("IntValue")
     Kills.Name = "Kills"
     Kills.Parent = Player
     Kills.Changed:Connect(function()
        print(Kills.Value)
     end)
end)

This should work fine.

1 Like

Oh yeah, didn’t even pick that up
@AridFights1

Look @AridFights1 , its not a dictionary,its a table of players, you cannot use Changed function on a table, you can use it mostly on an Instance, and trying to do it on tables may cause errors

for i, v in pairs(game.Players:GetPlayers()) do
	v:WaitForChild("Kills").Changed:Connect(function()
            print(v.Name)
            print(v.Kills.Value)
   end)
end

The reason why it does not print is, its not changing
Example: I do the same script like your script

Then
Table1

local players = {
  {Player = v,
    Kills = v.Kills},
   {Player = v,
    Kills = v.Kills}
}

Table2, both of these are same but this is easy to understand

local players = {
  {Player = AridFights1,
    Kills = 4},
   {Player = AridFights2,
    Kills = 1}
}

Explanation

When the player kills change, these values don’t change
You understood?

Listen, if a player kills change, the value in table2, which is also table1 does not change, but instead of keeping a number value in table, you can keep an instance, you can only use Changed event on a instance, the 2nd mistake you did is tried to use changed on a number value

Just like @ProgrammerDMD reply, this is also same like his reply

1 Like

So, it means even if I add an Instance to a table then it won’t perform any of it’s functions or Events?

1 Like