Datastore doesn't seem to save nor load

i thought i finally understood datastores :sob:

local ds = game:GetService("DataStoreService"):GetDataStore("PVZTR")
warn	("It does exist")
local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		table.insert(Data, Values)
		ds:UpdateAsync(plr.UserId, Data)
	end
end
local function LoadData(plr)
	ds:GetAsync(plr.UserId)
end

--loading data
local player
local CanSave = false
game.Players.PlayerAdded:Connect(function(plr)
	player = plr
	local folder = Instance.new("Folder", plr)
	folder.Name = "Values"
	local stage = Instance.new("IntValue", folder)
	stage.Name = "Stage"
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
		local success, err = pcall(function()
			LoadData(plr)
		end)
	if not success then
		local tries = 10
		while task.wait() do
			if tries ~= 0 then
				warn("Loading failed, retrying. Tries until rejoin:", tries)
				tries -= 1
				LoadData(plr)
			elseif tries == 0 then
				warn("rejoining.")
				game:GetService("TeleportService"):Teleport(game.PlaceId, plr)
			elseif success then
				print("Loaded")
				CanSave = true		
			end
		end
	end
end)

--saving data

game:BindToClose(function()
	warn("Trying to save")
	if CanSave then
		print("Data is not corrupted.")
		local success, err = pcall(function()
				SaveData(player)
			end)
		if not success then
			warn("Didn't save, retrying.")
			repeat SaveData(player) until success
		elseif success then
			print("Saved!")		
			end
		end
	end)

there has to be an issue /w the if statements but i can’t figure it out i am too dumb. it’s singleplayer btw

1 Like

The end keyword before the last elseif success then statement is unnecessary and causes a syntax error ._.

3 Likes

I dont know for sure but it looks like you forgot to set async

2 Likes

I don’t understand why you need to repeat the save everywhere if it fails, as it won’t really help and isn’t necessary if you make your game right (in terms of database and scripts)

local ds = game:GetService("DataStoreService"):GetDataStore("PVZTR")

local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		table.insert(Data, Values)
	end
	ds:UpdateAsync(plr.UserId, Data)
end

local function LoadData(plr)
	return ds:GetAsync(plr.UserId)
end

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "Values"
	local stage = Instance.new("IntValue", folder)
	stage.Name = "Stage"
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	local success, data = pcall(function()
		LoadData(plr)
	end)
	if not success then
		local tries = 0
		while task.wait(math.pow(2, tries)) do
			if tries>=3 then
				warn("rejoining.")
				game:GetService("TeleportService"):Teleport(game.PlaceId, plr)
				break
			else
				tries+=1
				success, data = pcall(function()
					LoadData(plr)
				end)
				if success then
					print("Loaded")
					break
				end
			end
		end
	end
	
	--set data for player (stage.Value=data.stage - EXAMPLE!!!)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	SaveData(plr)
end)

--saving data

game:BindToClose(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		SaveData(v)
	end
end)
3 Likes

ok but Updateasync throws this error: Unable to cast value to function
also you forgor about pcalls for saving (which are very much needed)

1 Like
ds:UpdateAsync(plr.UserId, function(pastData)
    return Data
end)

Docs: GlobalDataStore | Roblox Creator Documentation

2 Likes

I wouldn’t say what could be the reason for not saving? You also need to remember about the restrictions on Update / Set Async, and while the code is being executed, the player will simply leave the game.

1 Like

what does the function do exactly?

1 Like

i never said that was the issue, but more as an issue if it fails to save. better be safe than sorry. also loading isn’t bad as long as you have measures if it fails, like not saving but you should pcall saving since that is really important.

1 Like

Replaces old data with new data.

1 Like

This is your business, but I do not recommend doing it as it was (try to save many times)

1 Like

thanks, but now "Expected ) to close ( at column 16, got ‘function’ which was copy and pasted from the snippet you gave.

local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		table.insert(Data, Values)
	end
	ds:UpdateAsync(plr.UserId function(pastData)
    return Data
	end
end
1 Like

You missed ). And I’m not sure if the script will work since you are saving the Instance to a table. Use it:

local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		Data[Values.Name] = Values.Value
	end
	ds:UpdateAsync(plr.UserId function(pastData)
    return Data
	end)
end
2 Likes

small question, haven’t tested it yet: WHERE ARE THE VALUES ADDED TO THE TABLE?!
my brain is gonna implode

same error btw

local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		Data[Values.Name] = Values.Value
	end
	ds:UpdateAsync(plr.UserId function(pastData)
		return Data
	end)
end
1 Like

You missed ,

ds:UpdateAsync(plr.UserId, function(pastData)
		return Data
	end)
1 Like

thanks but you did :nerd_face:
“ds:UpdateAsync(plr.UserId function(pastData)”

1 Like

doesn’t save :sob: i am raging. yes i am changing values on the server.

1 Like

Try to write the value that you save and get

local function SaveData(plr)
	local Data = {}
	for _, Values in pairs (plr.Values:GetChildren()) do
		Data[Values.Name] = Values.Value
	end
	print(Data)
	ds:UpdateAsync(plr.UserId, function(pastData)
		return Data
	end)
end

and
replace --set data for player (stage.Value=data.stage - EXAMPLE!!!) to print(data)

1 Like

prints nil and nothing loads. asdasd

1 Like

And when saving prints what you need?

1 Like