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

My datastore isn't loading properly, what's going wrong?

I have a datastore for Diamonds, Levels, and Exp. Diamonds load just fine, the issue is with Levels and Exp. For some reason, they 'save' (as in it prints in output that they saved just fine) but they don't load as the new values. I've already posted this question, but I've changed a few things based on suggestions. So, I was told to replace part of my code and to put them all on one script for efficiency. Sadly, this didn't fix my issue so I figured I would reword it. I'm going to give all of the 200+ lines of code to see what's going wrong as a whole.

To be extra clear, the issue is either that it's not actually saving, or something is making it impossible to load. Something to keep in mind, I have another script that may be causing issues. It adds value to Exp until it reaches a certain number, then resets Exp to 0 and adds a Level. I'm not sure if I need to use a different method for that though, and if that's the issue.

Datastore Script:

-- Datastores
local DataStoreService = game:GetService("DataStoreService")

local dsDiamonds = DataStoreService:GetDataStore("ds_Diamonds")
local dsLevels = DataStoreService:GetDataStore("ds_Levels")
local dsExp = DataStoreService:GetDataStore("ds_Exp")

-- Adding stats and checking for data
game.Players.PlayerAdded:Connect(function(player)
	
	-- Folders
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- Making Storage
	local Diamonds = Instance.new("IntValue")
	Diamonds.Name = "Diamonds"
	Diamonds.Value = 1000
	Diamonds.Parent = leaderstats
	
	local Levels = Instance.new("IntValue")
	Levels.Name = "Levels"
	Levels.Value = 1
	Levels.Parent = leaderstats
	
	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	Exp.Parent = leaderstats
	
	-- Create Data Vars
	local datadiamonds
	local datalevels
	local dataexp
	
	-- Check for Data
	local success, errormessage = pcall(function()
		datadiamonds = dsDiamonds:GetAsync(player.UserId.."-diamonds")
		datalevels = dsLevels:GetAsync(player.UserId.."-levels")
		dataexp = dsExp:GetAsync(player.UserId.."-exp")
	end)
	if success then
		Diamonds.Value = datadiamonds
		Levels.Value = datalevels
		Exp.Value = dataexp
	else
		print("There was an error getting data")
		warn(errormessage)
	end
	
end)

-- Saving when player leaves
game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		dsDiamonds:SetAsync(player.UserId.."-diamonds", player.leaderstats.Diamonds.Value)
		dsLevels:SetAsync(player.UserId.."-levels", player.leaderstats.Levels.Value)
		dsExp:SetAsync(player.UserId.."-exp", player.leaderstats.Exp.Value)
	end)
	if success then
		print("Data Saved Successfully")
	else
		print("Data could not be saved")
		warn(errormessage)
	end
	
end)

game:BindToClose(function()
	-- When ready to shutdown
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Server shutdown, join back in a moment!")
		end
	end
	wait(5)
end)

Exp->Level Script:

local t = 0
local timeneeded = 1
local player = game.Players.LocalPlayer

while true do
	t = t + 1
	wait(1)
	
	if t == timeneeded then
		--experience + 10
		player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 1
		t = 0
	end
	
	if player.leaderstats.Exp.Value >= 20 then
		player.leaderstats.Exp.Value = 0
		player.leaderstats.Levels.Value = player.leaderstats.Levels.Value + 1
	end

end

3 comments

0

That won't save because you are giving points through the client not the server. That means only you can see your value while the server sees a value of 0, use remote events or add value from the server. Also I recommend saving data in a single datastore as a table, much more efficient!

0

Do you have anything I could read so I could figure this out? I'm not sure how to use remote events or make it into a table.

0

I figured it out thank you! I'm still not sure how to make it all in one datastore though?

1 answer

@greatneil80 Told me that the data was saved through the client, not the server, so I put the script I was using into my datastore script and it finally works!

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