Datastore sets every value to 0

I’m trying to save all of the values in the player, this is the script in server script service, for some reason it sets every value to 0.

	
	local playerUserId = "Player_"..player.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(playerUserId)
	end)
	
	if success then
		Coins.Value = data
		Level.Value = data
		Stage1BestTime.Value = data
		Stage2BestTime.Value = data
		Stage3BestTime.Value = data
		Stage4BestTime.Value = data
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local dataCoins = player.leaderstats.Coins.Value
	local dataLevel = player.leaderstats.Level.Value
	local dataBestTime1 = player.Times.Stage1.BestTime.Value
	local dataBestTime2 = player.Times.Stage2.BestTime.Value
	local dataBestTime3 = player.Times.Stage3.BestTime.Value
	local dataBestTime4 = player.Times.Stage4.BestTime.Value
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(playerUserId, dataCoins)
		dataStore:SetAsync(playerUserId, dataLevel)
		dataStore:SetAsync(playerUserId, dataBestTime1)
		dataStore:SetAsync(playerUserId, dataBestTime2)
		dataStore:SetAsync(playerUserId, dataBestTime3)
		dataStore:SetAsync(playerUserId, dataBestTime4)
	end)
	
	if success then
		print("Saved: "..player.Name..playerUserId)
	end
end)

It is because you haven’t specified the index from table that you get from :GetAsync()

It should be something like data[1] etc.

1 Like

Ah alright, I’ve done this, should work, thank you for the reply!

	if success then
		Coins.Value = data[1]
		Level.Value = data[2]
		Stage1BestTime.Value = data[3]
		Stage2BestTime.Value = data[4]
		Stage3BestTime.Value = data[5]
		Stage4BestTime.Value = data[6]
	end
end)

Yes your code was previously trying to assign table to those values.

1 Like

It still doesn’t work, it says “Attempt to index number with number”

Can you try to print(data)?? And lmk what you got

1 Like

It prints 0 - Server - PlayerJoin:86, 0 is the value of coins, so I think data is only equal to coins.Value, i’m not sure how I would make the data as a table then save it.

Don’t worry, I’ve fixed it now.

You’re only getting 1 datastore while you’re saving 6. Instead of doing this, just use a table and use @caviarbro’s method. Also, please provide full code next time.

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local dataStore = DSS:GetDataStore("DataSave")

local function SaveData(player)
	local Leaderstats = player.leaderstats
	local Times = player.Times
	
	local ValuesToSave = {
		Leaderstats.Coins.Value;
		Leaderstats.Level.Value;
		Times.Stage1.BestTime.Value;
		Times.Stage2.BestTime.Value;
		Times.Stage3.BestTime.Value;
		Times.Stage4.BestTime.Value
	}

	pcall(function()
		dataStore:SetAsync(player.UserId, ValuesToSave)
	end)
end

Players.PlayerAdded:Connect(function(player)
	local Coins, Level, Stage1BestTime, Stage2BestTime, Stage3BestTime, Stage4BestTime -- your stats
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId) or { }
	end)

	if success then
		Coins.Value = data[1] or 0
		Level.Value = data[2] or 0
		Stage1BestTime.Value = data[3] or 0
		Stage2BestTime.Value = data[4] or 0
		Stage3BestTime.Value = data[5] or 0
		Stage4BestTime.Value = data[6] or 0
	end
end)


Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
	for _, player in ipairs(Players:GetPlayers()) do
		SaveData(player)
	end
end)
2 Likes

Thank you for the reply, I didn’t provide the full code because the script in 400 lines.