Datastore Error I can't fix it

Hello there,

Okay ima just get straight into it. So I made this datastore script for my game and I don’t know why but every time I leave the game i get a warning in the output saying this.

I don’t know how to fix this or what I have done wrong.

local DataStore = game:GetService("DataStoreService")
local PlayerCash = DataStore:GetDataStore("PlayerCash")
local PlayerLoan = DataStore:GetDataStore("PlayerLoan")

function Leaderstats(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "leaderstats"

	local pounds = Instance.new("IntValue", stats)
	pounds.Name = "Pounds"
	pounds.Value = 0

	local bounty = Instance.new("NumberValue", stats)
	bounty.Name = "Bounty"
	bounty.Value = 0

	local kms = Instance.new("IntValue", stats)
	kms.Name = "Kms"
	kms.Value = 0

	local loan = Instance.new("IntValue", plr)
	loan.Name = "Loan"
	loan.Value = 0

	plr.CharacterAdded:Connect(function()
		saving(plr)
	end)
end

function OnPlayerAdded(player)
	wait(2)
	local plrkey = "id_"..player.UserId
	local loanKey = "loan_"..player.UserId
	local leaderstats = player:WaitForChild("leaderstats")

	local success, message = pcall(function()
		local data = PlayerCash:GetAsync(plrkey)
		local data2 = PlayerLoan:GetAsync(loanKey)

		if data and #data == 3 then
			leaderstats.Pounds.Value = data[1]
			leaderstats.Bounty.Value = data[2]
			leaderstats.Kms.Value = data[3]
		end

		if data2 then
			leaderstats.Loan.Value = data2
		end
	end)

	if not success then
		warn("Error loading data for player " .. player.Name .. ": " .. message)
	end
end

function saving(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local success, message = pcall(function()
			local data = {
				leaderstats.Pounds.Value,
				leaderstats.Bounty.Value,
				leaderstats.Kms.Value
			}

			PlayerCash:SetAsync("id_"..player.UserId, data)
			PlayerLoan:SetAsync("loan_"..player.UserId, leaderstats.Loan.Value)
		end)

		if not success then
			warn("Error saving data for player " .. player.Name .. ": " .. message)
		end
	end
end

function bindingwhenclosing()
	for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
		saving(player)
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	Leaderstats(plr)
	OnPlayerAdded(plr)
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
	saving(plr)
end)

game:BindToClose(bindingwhenclosing)


Please feel free to ask question’s and ect cause idk why It’s done this thank you :slight_smile:

I believe your issue with saving is due to the fact that it appears you are trying to store a table in a roblox data store. From my experiments data stores can not contain tables, but the way I got around this was encoding the table data’s to json and then decoding it to read the information.

2 Likes

I’ll have a look at it of trying to save it I’m currently working with my mate.

If you would like any samples I can show you snippets of the code I use to store the data In games of my own, hope this helps. If you have any questions feel free to ask

No no it’s okay I’ll make it so it saves the table

While I am not entirely sure why it didn’t save the data since after a brief review of your code, it all seems like it should work correctly, I do however want to point out that Datastore can in fact save tables without the need to JSONencode it, as long as they do not contain object references. If the table only has numbers, strings, booleans or more tables, you are fine.

2 Likes

The error is the one you get from datastore2. It doesn’t seem like you use datastore2, so you can probs just ignore it

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