Rather than update all the Values every whatever, update them when the ServerStorage Value fire their Changed events:
local pData = game:GetService("ServerStorage").PLAYER_DATA
game.Players.PlayerAdded:connect(function(player)
for _, statValue in pairs(player.leaderstats:GetChildren()) do
local pDataValue = pData[player.Name].leaderstats[statValue.Name]
pDataValue.Changed:connect(function()
statValue.Value = pDataValue.Value
end)
end
end)
By the way, you should look into correctly formatting your code.
The reason your code didn't work is your usage of GetChildren() on line 7.
local pData = game:GetService("ServerStorage").PLAYER_DATA
for _, v in pairs(player.leaderstats:GetChildren()) do
v.Value = pData[player.Name].leaderstats:GetChildren().Value
end
The GetChildren() on line 4 there is the same as line 7 in your code.
The issue is that GetChildren() returns an array of the children of pData[player.Name].leaderstats, not any one specific Value object like I think you were expecting. Because of this, .Value is trying to set v.Value to nil, since the "Value" index of a Table returned by GetChildren() is undefined.
In my first code block, I get the specific leaderstat connecting to the current v directly via its Name. Applying that concept to the second code block, we get this:
local pData = game:GetService("ServerStorage").PLAYER_DATA
for _, v in pairs(player.leaderstats:GetChildren()) do
v.Value = pData[player.Name].leaderstats[v.Name].Value
end
2 comments
Why did you do ""..player.Name.."" if there is nothing in those strings?
The name of the Folder is the players name, then it has stats inside of it, I'm trying to make the stats in player.leaderstats equal to the stats in the folder in server storage