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

Help with this DataStore?

Asked by 9 years ago

I have a script where player's have the ability to purchase upgrades through a currency earned while in game. I save the upgrades they purchase with the data store below.

There is a StringValue inserted into the player when they enter, and another StringValue is inserted into that one when they purchase an upgrade. The name of the StringValue is the name of the upgrade. When a player leaves, the script get's the children of the main StringValue called ValueHolder. It converts the names of the children into strings and places them in a table. When a player rejoins it get's the string's and gathers the weapons from Replicated storage.

So here's the problem. I need to convert the strings in the table back to the instances StringValue, and place them back in the value holder when a player enters. I tried doing that. But it hasn't been working. Can anyone help me out on how to do this correctly?

local Upgrades = game:GetService("DataStoreService"):GetDataStore("Tools")

game.Players.PlayerAdded:connect(function(playerEntered)
    local vTools = Upgrades:GetAsync(tostring(playerEntered.userId))
    if vTools then
        for i,v in pairs(vTools) do
            game.ReplicatedStorage[v]:Clone().Parent = playerEntered.Backpack
        end
    end
end)

game.Players.PlayerRemoving:connect(function(playerLeave)
    local vTools = {}
    for i,v in pairs(playerLeave.ValueHolder:GetChildren()) do
        table.insert(vTools, v.Name)
    end
    Upgrades:SetAsync(tostring(playerLeave.userId), vTools)
end)
0
Can you further explain what the problem is? As far as I can tell it inserts a value into the player for each upgrade they acquire and then uses that Strings name to insert it into a table which is later grabbed when they re-enter but I don't quite see what isn't working... An error from output window or console would be nice or a further explanation would help. VariadicFunction 335 — 9y
0
There's no errors, the issue is that when a player leaves a second time, the DataStore only saves the current string values in the ValueHolder. So if they buy an upgrade, leave, and then rejoin. The StringValue will no longer be there. Thus the DataStore won't save it the second time. I need to convert the table back to StringValues when a player re-enters. QuillStrike 60 — 9y

1 answer

Log in to vote
0
Answered by
noliCAIKS 210 Moderation Voter
9 years ago

You need to recreate the values when loading:

game.Players.PlayerAdded:connect(function(playerEntered)
    local valueHolder = playerEntered:WaitForChild("ValueHolder")
    local vTools = Upgrades:GetAsync(tostring(playerEntered.userId))
    if vTools then
        for i,v in pairs(vTools) do
            Instance.new("StringValue", valueHolder).Name = v
            game.ReplicatedStorage[v]:Clone().Parent = playerEntered.Backpack
        end
    end
end)
Ad

Answer this question