My datastore is a dumpster fire

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want this script to create two leaderstats called “Earned ($)” and “Spent($)”, and a folder called “leaderstats” to store them in the player. The script is located in ServerScriptService. When the player is added, the data is loaded using :GetAsync, and when the player is removed, the data is loaded using :SetAsync. The autoload was written by me with very limited scripting knowledge.

  2. What is the issue?
    This script was initially written by AlvinBlox a few years ago. I modified it a bit to include two leaderstats, but the script is a mess. The warnings work at random, when it does it says it saves properly but does not load.
    Additionally, the DataStore request gets queued when autosaving, which I thought would be fixed by using wait(10) but that doesn’t do anything.
    Other than that, there are no error messages.
    Yep, I’m in a pickle. Any help to fix this script would be much appreciated.
    AlvinBlox’s YouTube video can be found, I’ve attached the timestamp of the final script. Roblox DataStore Tutorial - Data Stores & Saving Data - Scripting Tutorial - YouTube

  3. What solutions have you tried so far?
    I’ve tried looking on the DevForum but was never really able to find anything helpful.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

--VARIABLES
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

--PLAYER ADDED
game.Players.PlayerAdded:Connect(function(player)
	
	
	--CREATE LEADERSTATS
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	
	--EARNED LEADERSTAT
	local moneyearned = Instance.new("IntValue")
	moneyearned.Name = "Earned ($)"
	moneyearned.Parent = leaderstats
	
	
	--SPENT LEADERSTAT
	local moneyspent = Instance.new("IntValue")
	moneyspent.Name = "Spent ($)"
	moneyspent.Parent = leaderstats
	
	
	--LOAD EARNED
	local earneddata
	local success, errormessage = pcall(function()
		earneddata = myDataStore:GetAsync(player.UserId.."Earned ($)")
	end)
	if success then
		moneyearned.Value = earneddata
	else
		print("There was an error when retrieving money earned data.")
		warn(errormessage)
	end
	
	
	--LOAD SPENT
	local spentdata
	local success, errormessage = pcall(function()
		spentdata = myDataStore:GetAsync(player.UserId.."Spent ($)")
	end)
	if success then
		moneyspent.Value = spentdata
	else
		print("There was an error when retrieving money spent data.")
		warn(errormessage)
	end
	
end)

--PLAYER REMOVED
game.Players.PlayerRemoving:Connect(function(player)
	
	
	--SAVE EARNED
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, player.leaderstats["Earned ($)"].Value)
	end)
	if success then
		print("Money earned data successfully saved.")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
	
	
	--SAVE SPENT
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId, player.leaderstats["Spent ($)"].Value)
	end)
	if success then
		print("Money spent data successfully saved.")
	else
		print("There was an error when saving money spent data.")
		warn(errormessage)
	end
	
end)


--AUTOSAVE
game.Players.PlayerAdded:Connect(function(player)
	while true do
		wait(10)
		myDataStore:SetAsync(player.UserId, player.leaderstats["Spent ($)"].Value)
		myDataStore:SetAsync(player.UserId, player.leaderstats["Earned ($)"].Value)
		

	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

So your problem is that you want to store multiple values in the leader stats and you want it to save? Also, I don’t think you create two leader stats, but you create multiple values in the leader stats.

1 Like

Yes, as explained in question one.

I think this video can help since you want multiple values in the leader stats with data stores, (I’m not 100% sure if this is still working, so if it isn’t, I’ll try to find another resource)

2 Likes

Create a Leaderstat Script and Place into ServerScriptService:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder", player) -- You can call its parent after the ,
	leaderstats.Name = "leaderstats"

	local moneyEarned = Instance.new("IntValue", leaderstats)
	moneyEarned.Name = "Earned ($)"

	local moneySpent = Instance.new("IntValue", leaderstats)
	moneySpent.Name = "Spent ($)"
end

-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

DataStore Script can Go as:

local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("DrPhilGame") -- CHANGE DATASTORE NAME

local function playerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		-- Defaults
		player.leaderstats["Spent ($)"].Value = Data["Spent"]
		player.leaderstats["Earned ($)"].Value = Data["Earned"]
	end
end

local function playerRemoved(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				-- Defaults
				Spent = player.leaderstats["Spent ($)"].Value,
				Earned = player.leaderstats["Earned ($)"].Value,
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	wait(3)
end)
3 Likes

Thank you, it works! “DrPhilGame” is an interesting datastore name choice though haha!

1 Like

Oops :sweat_smile:

That DataStore was ripped from a game I was creating :joy:

2 Likes

So for some reason, the data only saves for me. Trying on alt accounts, it doesn’t work in game. Here’s the script, maybe I broke something while adding additional stats.

--Script by Deadlox85854868.


local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("myDataStore")

local function playerAdded(player)
	wait(.5)
	local character = player.Character or player.CharacterAdded:Wait()
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		player.leaderstats["Spent ($)"].Value = Data["Spent"]
		player.leaderstats["Earned ($)"].Value = Data["Earned"]
		player.leaderstats["Played"].Value = Data["Played"]
		player.leaderstats["Time Played"].Value = Data["TimePlayed"]
	end
end

local function playerRemoved(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				
				Spent = player.leaderstats["Spent ($)"].Value,
				Earned = player.leaderstats["Earned ($)"].Value,
				Played = player.leaderstats["Played"].Value,
				TimePlayed = player.leaderstats["Time Played"].Value,
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	wait(3)
end)

Alt Accounts as in Roblox Studios provided ones or running the game through the website with a separate account?

Not the fix, but just minor adjustments.

local datastoreservice = game:GetService('DataStoreService')
local datastore = datastoreservice:GetDataStore("myDataStore")

local function playerAdded(player)
-- Removed Wait
	local character = player.Character or player.CharacterAdded:Wait()
	local Data = datastore:GetAsync(player.UserId)
	if Data then
		player.leaderstats["Spent ($)"].Value = Data["Spent"]
		player.leaderstats["Earned ($)"].Value = Data["Earned"]
		player.leaderstats["Played"].Value = Data["Played"]
		player.leaderstats["Time Played"].Value = Data["TimePlayed"]
	end
end

local function playerRemoved(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local success, errorMsg = pcall(function()
		datastore:UpdateAsync(player.UserId, function()
			return {
				Spent = player.leaderstats["Spent ($)"].Value,
				Earned = player.leaderstats["Earned ($)"].Value,
				Played = player.leaderstats["Played"].Value,
				TimePlayed = player.leaderstats["Time Played"].Value,
			}
		end)
	end)
	if errorMsg then
		print(errorMsg)
	end
end

game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoved)

game:BindToClose(function()
	task.wait(3) -- task.wait reduces throttling isssues
end)

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