Datastore doesn't seem to save nor load

yeah. loading is the issue. bruh

1 Like

I’m not sure if it saves it properly, try this

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

nope, but it doesn’t warn with OooOOO requests anymore.

1 Like
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"
	print(LoadData(plr))
	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)
1 Like

table prints 4, which is the value i set but the instance is still 0 bru

1 Like

So you haven’t set a value for them…

game.Players.PlayerAdded:Connect(function(plr)
	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
	if not data then data = {} end
	local folder = Instance.new("Folder", plr)
	folder.Name = "Values"
	local stage = Instance.new("IntValue", folder)
	stage.Name = "Stage"
	stage.Value = data.Stage or 1 --replace 1 to default value
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	level.Value = data.Level or 1 --replace 1 to default value
end)
1 Like

nothing changed aside from the default being 1 (which is needed lol)

1 Like

Can you please send your new script again and are you sure you are changing the values ​​on the server side?

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

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

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

game.Players.PlayerAdded:Connect(function(plr)
	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
	if not data then data = {} end
	local folder = Instance.new("Folder", plr)
	folder.Name = "Values"
	local stage = Instance.new("IntValue", folder)
	stage.Name = "Stage"
	stage.Value = data.Stage or 1
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	level.Value = data.Level or 1
	end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, err = pcall(function()
		SaveData(plr)
	end)
	if not success then
		repeat SaveData(plr) until success
	end
end)

--saving data

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local success, err = pcall(function()
			SaveData(plr)
		end)
		if not success then
			repeat SaveData(plr) until success
		end
	end
end)
1 Like

Try it:

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

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

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

game.Players.PlayerAdded:Connect(function(plr)
	local success, data = pcall(function()
		return LoadData(plr)
	end)
	print(success)
	print(data)
	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()
					return LoadData(plr)
				end)
				if success then
					print("Loaded")
					break
				end
			end
		end
	end
	if not data then data = {} end
	local folder = Instance.new("Folder", plr)
	folder.Name = "Values"
	local stage = Instance.new("IntValue", folder)
	stage.Name = "Stage"
	stage.Value = data.Stage or 1
	local level = Instance.new("IntValue", folder)
	level.Name = "Level"
	level.Value = data.Level or 1
end)

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

--saving data

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		pcall(function()
			SaveData(plr)
		end)
	end
end)
1 Like

AYYYYYYYYY what changed? yes, we’re using math.pow and default vals. and the weird if not data then data = {} end

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