Hello I'm working on a DataStore script for a game I plan to release real soon and to make updating it in the future a lot easier I need to figure out how to save any newly created values in the datastore. For example whenever a player buys a specific item, instead of having a value already written in the DataStore script I want to create the specific instance of the value and it's value and have those saved instead. Is this doable and if so how would I go about doing this?
Here's some of my current DataStore script:
local key = "user-"..plyr.userId local storeditems = DataStore:GetAsync(key) if storeditems then money.Value = storeditems[1] shirt.Value = storeditems[2] pants.Value = storeditems[3] for i,v in pairs(storage:GetChildren()) do v.Value = storeditems[i+3] end else repeat wait(5*60) local items = {plyr.Data.Money.Value,plyr.Data.CurrentShirt.Value,plyr.Data.CurrentPants} for i,v in pairs(plyr.Data.Storage:GetChildren()) do table.insert(items,i+3,plyr.Data.Storage[v.Name].Value) end --Save any newly bought items? print("Saving Data...") DataStore:SetAsync(key, items) DataStore:SetAsync(plyr.userId,os.time()) wait(2) print("Data Saved!") until plyr == nil end
Thanks in advance for any help!
I don't really see what your trying to ask, but you can try this:
function onPlayerEntered(player) wait() player:WaitForDataReady() repeat wait() until player:FindFirstChild("leaderstats") if player.DataReady then if player:findFirstChild("leaderstats") then local score = player.leaderstats:GetChildren() for i = 1,#score do local ScoreLoaded = player:LoadNumber(score[i].Name) wait() if ScoreLoaded ~= 0 then score[i].Value = ScoreLoaded end end end end end function onPlayerLeaving(player) if player:findFirstChild("leaderstats") then local score = player.leaderstats:GetChildren() for i = 1,#score do player:SaveNumber(score[i].Name,score[i].Value) end end end game.Players.PlayerAdded:connect(onPlayerEntered) game.Players.PlayerRemoving:connect(onPlayerLeaving)
Sorry if it dont work, but dont down vote me please, but upvote if it worked!