Confused with table mechanism [solved]

  1. What is the issue?
    Error: Index nil with leaderstats

This is the part of the script that has the issue. “Winners” is a table and I’m trying to get every player from that table and give them the win. It gives the error mentioned above.

for i,v in pairs(Winners) do
		
		Winners[v].leaderstats.Wins.Value += 1
		
	end

Try this maybe

for i,v in pairs(Winners) do
		
		local plr = game.Players:FindFirstChild(v)
        if plr then
           plr.leaderstats.Wins.Value += 1
        end
		
	end
3 Likes

A full code sample:
local game = script.Parent
local Players = game.Players
local ServerScriptService = game:GetService(“ServerScriptService”)
local ServerStorage = game:GetService(“ServerStorage”)
local ServerStorage = game:GetService(“ServerStorage”)
local Workspace = game:GetService(“Workspace”)
local StarterGui = game:GetService(“StarterGui”)

game.ChildRemoved:Connect(function(child)
if child.Name == “Humanoid” then
child.Parent:Destroy()
end
end
)

function PlayersJoin()
for i,v in pairs(Players:GetPlayers()) do
local player = v
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Leaderstats = Instance.new(“IntValue”)
Leaderstats.Name = “Wins”
Leaderstats.Parent = leaderstats
Leaderstats.Value = 0
end
end

local Win = Instance.new(“Sound”)
Win.SoundId = “rbxassetid://673087839”
Win.Parent = Workspace
Win.Volume = 2

local function getWinners()
local Winners = {}
for i,v in pairs(game.Players:GetPlayers()) do
if v.PlayerGui:FindFirstChild(“HungerGui”) then
table.insert(Winners, v)
end
end
return Winners
end

function WinGame()
local Winners = getWinners()
for i,v in pairs(Winners) do
local sound = Win:Clone()
sound.Parent = v.PlayerGui
sound:Play()
end

for i,v in pairs(Winners) do
print(v.Name)
Winners[v].leaderstats.Wins.Value += 1
end

for i,v in pairs(Winners) do
v.PlayerGui.HungerGui:Destroy()
v.PlayerGui.WinnerGui.Text = “Winner”
end

for i,v in pairs(Workspace:GetChildren