Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastore not saving in-game or in studio?

So I got this script from the DevForum, but the datastore doesn't work in studio(i have the api thingy on) and in-game. I gave myself 69,420 bobux (haha funny number) and when I leave, it prints that the data didn't save. Please help.

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Money.Value; -- First value from the table
		player.leaderstats.Coins.Value -- Second value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

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

	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Money = Instance.new("IntValue")
	Money.Name = "Bobux"
	Money.Parent = leaderstats
	
	while true do
		wait(60) -- one second
		Money.Value = Money.Value + 40 -- one prestige/cash per second
	end

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore

	end)

	if success then -- If there were no errors and player loaded the data

		Money.Value = data[1] -- Set the money to the first value of the table (data)

	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

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

	if success then
		print("Data has been saved")
	else
		warn("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

3 comments

0

It should be accompanied with a warning.

0

What do you mean?

0

On line 20 you have [ warn(err) ] which should show you a warning in the output in yellow text, this is error that happened during the function.

1 answer

✓ Accepted answer

This script could be alot shorter, but it doesnt matter anyways. So i noticed that you were saving the stat named Money but you have a stat named Bobux, so that was one problem. Another was that you were not loading in the money how it should be loaded. But i fixed it. Hope this helps!

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("EconomyDataStore")

local function saveData(player)

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, {player.leaderstats.Bobux.Value})
	end)

	if success then 
		print("Data has been saved!")
	else
		print("Data hasn't been saved!  " .. err)       
	end
end

game.Players.PlayerAdded:Connect(function(player) 
	wait(1)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local bobux = Instance.new("IntValue")
	bobux.Name = "Bobux"
	bobux.Parent = leaderstats

	local data 
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) 

	end)
	
	save1 = player.leaderstats.Bobux

	if success then 

		save1.Value = data[1] 

	else
		print("The player has no data!") 
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player) 
	end)

	if success then
		print("Data has been saved")
	else
		warn("Data has not been saved!"  .. err)
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do 
		local success, err  = pcall(function()
			saveData(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!  " .. err)
		end
	end
end)

5 comments

0

That's weird, it says it saves but it still doesn't save

0

It does save, i tested it.

0

It saves and loads.

0

Do you have to wait for a couple of minutes for it to save?

0

I tested it. you do not need to wait, it saves it, and loads it

This discussion is preserved as it was. New answers, comments, and votes are closed.