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)
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)