Help with Data store

first time using a data store, so I made this script, but it only saves Money. How can I solve this problem?

ServerScript

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local savevalue = plr.leaderstats.Money
	local savevalue2 = plr.Stats.Rock
	local savevalue3 = plr.Stats.Coal
	local savevalue4 = plr.Stats.Iron
	local savevalue5 = plr.Stats.Gold
	local savevalue6 = plr.Stats.Diamond
	local savevalue7 = plr.Stats.Crystall
	local savevalue8 = plr.Stats.BlueCrystall
	local savevalue9 = plr.Stats.RedCrystall

	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
		savevalue2.Value = GetSaved[2]
		savevalue3.Value = GetSaved[3]
		savevalue4.Value = GetSaved[4]
		savevalue5.Value = GetSaved[5]
		savevalue6.Value = GetSaved[6]
		savevalue7.Value = GetSaved[7]
		savevalue8.Value = GetSaved[8]
		savevalue9.Value = GetSaved[9]

	else
		local NumbersForSaving = {savevalue.Value, savevalue2.Value, savevalue2.Value, savevalue3.Value, savevalue4.Value, savevalue5.Value, savevalue6.Value, savevalue7.Value, savevalue8.Value, savevalue9.Value}
		DS:GetAsync(plrkey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value, plr.Stats.Rock.Value, plr.Stats.Rock.Value, plr.Stats.Coal.Value, plr.Stats.Iron.Value, plr.Stats.Gold.Value, plr.Stats.Diamond.Value, plr.Stats.Crystall.Value, plr.Stats.BlueCrystall.Value, plr.Stats.RedCrystall.Value})
end)

Thank you!

2 Likes

What does DataStore return to you?

hmm… i think my data? or I misunderstood the question

Add

local GetSaved = DS:GetAsync(plrkey)
print("GetSaved")

And show the result of what you got

image

Oops, made a little mistake.

local GetSaved = DS:GetAsync(plrkey)
print(GetSaved)

image

Open this table, what will be there?

image

Are these the values that were supposed to be saved? Also, I have a question myself: Does the creation of all variables and values happen in another script?

Yes, it was necessary to save this data. And yes, the creation of variables occurs in another script

Try creating all of this in one script; it will be better. Also, give it a try

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",leaderstats)
	Money.Name = "Money"
	Money.Value = 0
	
	local StatsPlayer = Instance.new("Folder",plr)
	StatsPlayer.Name = "Stats"
	
	local Rock = Instance.new("IntValue",StatsPlayer)
	Rock.Name = "Rock"
	Rock.Value = 0
	local Coal = Instance.new("IntValue",StatsPlayer)
	Coal.Name = "Coal"
	Coal.Value = 0
	local Iron = Instance.new("IntValue",StatsPlayer)
	Iron.Name = "Iron"
	Iron.Value = 0
	local Gold = Instance.new("IntValue",StatsPlayer)
	Gold.Name = "Gold"
	Gold.Value = 0
	local Diamond = Instance.new("IntValue",StatsPlayer)
	Diamond.Name = "Diamond"
	Diamond.Value = 0
	local Crystall = Instance.new("IntValue",StatsPlayer)
	Crystall.Name = "Crystall"
	Crystall.Value = 0
	local BlueCrystall = Instance.new("IntValue",StatsPlayer)
	BlueCrystall.Name = "BlueCrystall"
	BlueCrystall.Value = 0
	local RedCrystall = Instance.new("IntValue",StatsPlayer)
	RedCrystall.Name = "RedCrystall"
	RedCrystall.Value = 0
	local plrkey = "id_"..plr.userId
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		Money.Value = GetSaved[1]
		Rock.Value = 	GetSaved[2]
		Coal.Value = GetSaved[3]
		Iron.Value = GetSaved[4]
		Gold.Value = GetSaved[5]
		Diamond.Value = GetSaved[6]
		Crystall.Value = GetSaved[7]
		BlueCrystall.Value = GetSaved[8]
		RedCrystall.Value = GetSaved[9]
	end
end)
1 Like

I tried to do as you said and got a script like this, but it still does not save the entire date

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",leaderstats)
	Money.Name = "Money"
	Money.Value = 0
	
	local StatsPlayer = Instance.new("Folder",plr)
	StatsPlayer.Name = "Stats"
	
	local Rock = Instance.new("IntValue",StatsPlayer)
	Rock.Name = "Rock"
	Rock.Value = 0
	local Coal = Instance.new("IntValue",StatsPlayer)
	Coal.Name = "Coal"
	Coal.Value = 0
	local Iron = Instance.new("IntValue",StatsPlayer)
	Iron.Name = "Iron"
	Iron.Value = 0
	local Gold = Instance.new("IntValue",StatsPlayer)
	Gold.Name = "Gold"
	Gold.Value = 0
	local Diamond = Instance.new("IntValue",StatsPlayer)
	Diamond.Name = "Diamond"
	Diamond.Value = 0
	local Crystall = Instance.new("IntValue",StatsPlayer)
	Crystall.Name = "Crystall"
	Crystall.Value = 0
	local BlueCrystall = Instance.new("IntValue",StatsPlayer)
	BlueCrystall.Name = "BlueCrystall"
	BlueCrystall.Value = 0
	local RedCrystall = Instance.new("IntValue",StatsPlayer)
	RedCrystall.Name = "RedCrystall"
	RedCrystall.Value = 0
	local plrkey = "id_"..plr.userId
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		Money.Value = GetSaved[1]
		Rock.Value = 	GetSaved[2]
		Coal.Value = GetSaved[3]
		Iron.Value = GetSaved[4]
		Gold.Value = GetSaved[5]
		Diamond.Value = GetSaved[6]
		Crystall.Value = GetSaved[7]
		BlueCrystall.Value = GetSaved[8]
		RedCrystall.Value = GetSaved[9]
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value, plr.Stats.Rock.Value, plr.Stats.Rock.Value, plr.Stats.Coal.Value, plr.Stats.Iron.Value, plr.Stats.Gold.Value, plr.Stats.Diamond.Value, plr.Stats.Crystall.Value, plr.Stats.BlueCrystall.Value, plr.Stats.RedCrystall.Value})
end)

table is same
image

does it work in roblox studio?

Firstly, if you’re testing this in Studio, there might be errors because the server shuts down faster than it can save the data. Therefore, it’s better to test either through the Test tab where both the server and client are launched simultaneously, allowing you to safely check there, or directly in the game.
Dev1

1 Like

yeeep, its works. Thank you so much!

1 Like

Just thought I’d add a side note:
If you want to fix the issue where the game shuts down faster than it can save the data, use a BindToClose function. It might also prevent this issue in the actual game itself.

local runService = game:GetService("RunService")
local players = game:GetService("Players")

game:BindToClose(function()
    if runService:IsStudio() then task.wait(3) return nil end
    for _, player in ipairs(players:GetPlayers()) do
        --save data here
    end
    task.wait(3)
end)

(also, use a pcall when accessing the data store).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.