Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Replicating multiple values at once?

Asked by 7 years ago

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)
0
Why did you do ""..player.Name.."" if there is nothing in those strings? hiimgoodpack 2009 — 7y
0
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 greybird700 63 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

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

0
The values already exist, I'm trying to replicate the values that exist into the player but not clone them, I'm trying to take their values and put them in the other values greybird700 63 — 7y
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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

Answer this question