Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Saving newly created Values in a DataStore? [Closed]

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!

2 comments

0

I fixed your tabbing. Please do this yourself in the future.

0

That doesn't really help.... My tabbing was fine, makes it easier to read for me. Nvm I figured out a solution.

1 answer

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!

3 comments

1

I downvoted because you didn't explain what you changed, or why you changed it. If you don't explain what you did and why, people can't learn, and this becomes a bad answer.

1

The code is also awful.

0

Data persistence is deprecated.

This discussion is preserved as it was. New answers, comments, and votes are closed.