Datastore help - multiple saves (and nothing saves)

I’m writing datastore scripts over and over to try to learn how to write them without continuously looking up tutorials. I noticed the coins don’t save but there isn’t any errors only it saves multiple times (I’m assuming that’s the issue). I followed a tutorial but I never copy pasted and changed variables and names to suit what I wanted to save. Not sure where the problem lies.

image

---- Datastores and leaderstats ----

---- Variables ----
local runService = game:GetService("RunService")
local dataStore = game:GetService("DataStoreService")
local coinsData = dataStore:GetDataStore("Coins_01")

local function saveData(player) -- function that saves the data
	
	local tableToSave = {
		player.leaderstats.Coins.Value, -- first value of the table
		player.leaderstats.Rank.Value -- second value of the table
	}
	-- Save Data --
	local success, errorMessage = pcall(function()
		coinsData:SetAsync(player.UserId, tableToSave) -- saving the data to player and table we need to save
	end)
	
	if success then -- if the data is saved
		print("Successfully saved player data!")
	else -- if there is an error
		print("Data have not been saved!")
		warn(errorMessage)
	end
end

game.Players.PlayerAdded:Connect(function(player) -- when a player joins the game

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	local rank = Instance.new("StringValue")
	rank.Name = "Rank"
	rank.Parent = leaderstats
	rank.Value = "Noob"
	
	while true do
		if player and coins then
			coins.Value += 1
			task.wait(2)
		end
	end
	-- Load Data --
	local data -- data is the table that's saved
	local success, errorMessage = pcall(function()
		data = coinsData:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- if no errors and the player has data
		coins.Value = data[1] -- set the coins to the first value of the table (data)
		rank.Value = data[2] -- set the rank to the second value of the table (data)
	else -- The player didn't load any data, probably a new player
		print("The player has no data!") -- Default set to "0"
	end
end)

---- When player leaves game ----
game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMessage = pcall(function()
		saveData(player) -- save the data
	end)

	if success then
		print("Successfully saved player data!")
	else
		print("Data have not been saved!")
		warn(errorMessage)
	end
end)
---- When server shuts down ----
if not runService:IsStudio()then
	game:BindToClose(function()
		for _, player in pairs(game.Players:GetPlayers()) do -- loops through all players
			local success, errorMessage = pcall(function()
				saveData(player) -- save the data
			end)
			
			if success then
				print("Successfully saved player data!")
			else
				print("Data was not saved!")
				warn(errorMessage)
			end
		end
	end)
end

1 Like

The issue is that you have a while true do loop inside of the PlayerAdded function right before you load the data. This causes the script to never reach whatever is after the while loop, since it’s re-running that loop the whole time. You have to either put that part of the script inside of another script and remove it from here, or run it inside of a task.spawn():

task.spawn(function()
	while true do
		if player and coins then
			coins.Value += 1
			task.wait(2)
		end
	end
end)

This allows the script to continue running, successfully loading the player’s data.

By the way, it’s not saving twice, you just have two print statements when it saves once: one inside of your saveData() function and another inside of the PlayerRemoving function.

1 Like

Ty I’ll be sure to do that. I’ve shut down for the night (going to sleep) so if it works tomorrow I’ll be sure to credit you. Ty for the reply.

Edit: Worked perfectly, tyvm! I added in the line to make sure it was working and saving properly.
print(“The player has “…data[1]…” coins and “…data[2]…” rank!”)

1 Like

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