Data problem, help please

local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local mainStore = dataStoreService:GetDataStore("Main")

players.PlayerAdded:Connect(function(player)
	local ld = Instance.new("Folder", player)
	ld.Name = "leaderstats"

	local hld = Instance.new("Folder", player)
	hld.Name = "HideLeaderstats"

	local coins = Instance.new("IntValue", ld)
	coins.Name = "Coins"
	coins.Value = 0

	local timeCoins = Instance.new("IntValue", hld)
	timeCoins.Name = "TimeCoins"
	timeCoins.Value = 0

	local clickPower = Instance.new("IntValue", hld)
	clickPower.Name = "ClickPower"
	clickPower.Value = 1
	
	local success, data = pcall(function()
		return mainStore:GetAsync(player.UserId)
	end)
	
	if success then
		coins.Value = data or 0
		timeCoins.Value = data or 0
		clickPower.Value = data or 1
	else
		print("data failed to load")
	end
end)

players.PlayerRemoving:Connect(function(player)
	local ld = player:WaitForChild("leaderstats")
	local hld = player:WaitForChild("HideLeaderstats")

	local coins = ld:WaitForChild("Coins")
	local timeCoins = hld:WaitForChild("TimeCoins")
	local clickPower = hld:WaitForChild("ClickPower")
	
	local success, err = pcall(function()
		return mainStore:SetAsync(player.UserId, coins.Value, timeCoins.Value, clickPower.Value)
	end)
	
	if not success then
		print(err)
	end
end)

When I leave out of the game, it says this:
Unable to cast to Array

It’s this line right here:

if not success then
		print(err)
	end

Is that why my data is not being saved and it’s writing this problem?

The actual error is on this line here:

return mainStore:SetAsync(player.UserId, coins.Value, timeCoins.Value, clickPower.Value)

Instead of having a tuple of things to save, you should instead put them all into an array, for example:

{coins.Value, timeCoins.Value, clickPower.Value}

or even a dictionary:

{
    ["coins"] = coins.Value,
    ["timeCoins"] = timeCoins.Value,
    ["clickPower"] = clickPower.Value
}

You will also need to change the data loading code.

If you use an array:

coins.Value = data[1] or 0
timeCoins.Value = data[2] or 0
clickPower.Value = data[3] or 1

If you use a dictionary:

coins.Value = data["coins"] or 0
timeCoins.Value = data["timeCoins"] or 0
clickPower.Value = data["clickPower"] or 1
1 Like

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