So, basically what I am doing here is trying to take values from a Folder in ServerStorage and replicate the values over to player.leaderstats every 0.1 second but I'm not sure how to get every value (since they're all different, it wouldn't be GetChildren I'm assuming.. but that's what I tried and Idk a solution?)
game.Players.PlayerAdded:connect(function(player) while true do wait(0.1) for i,v in pairs(player:WaitForChild("leaderstats"):GetChildren()) do v.Value = (game.ServerStorage["PLAYER_DATA"]:WaitForChild(""..player.Name.."").leaderstats:GetChildren().Value) end end end)
If you're trying to get multiple values try using instance.new like I have here
game.Players.PlayerAdded:connect(function(player) local ShopItems = Instance.new("Folder",player) ShopItems.Name = 'ShopItems' local trail1 = Instance.new("StringValue", ShopItems) trail1.Name = "ParticleEquipped" trail1.Value = 'false' local trail2 = Instance.new("StringValue", ShopItems) trail2.Name = "Superfast" trail2.Value = 'false' local trail3 = Instance.new("StringValue", ShopItems) trail3.Name = "Sparkles" trail3.Value = 'false'
Obviously replace the names and values etc. Changing "StringValue" to "IntValue" would allow numbers instead of words
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) -- You should create the leaderstats here, there's no reason to use two different connections to PlayerAdded for this, since they're operating on the same data. -- To that end, this code will probably not work if you don't ensure the Value objects and Player Data entries are created before it runs. 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