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

Can somebody help me fix a logic error in my datastorage script?

I am new to working with data storage and I can get one data store working by itself, for example having a "coins" stat for all players that can save and it runs perfectly. However now I am trying to add a second data score for player xp and I am having some issues. There are no reported errors and I can't find any grammatical errors so I think I may be doing this incorrectly. The leaderstats work and the XP shows up for the player, but it will not save. The "bucks" value still saves, so I am at a loss. Any help would be appreciated.

local bucksDataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
local xpDataStores = game:GetService("DataStoreService"):GetDataStore("XPDataStore")

local defaultBucks = 15
local defaultXP = 0

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local bucks = Instance.new("IntValue")
	bucks.Name = "Bucks"
	bucks.Value = defaultBucks
	bucks.Parent = leaderstats
	
	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Value = defaultXP
	xp.Parent = leaderstats
	
	
	-- Datastores
	
	local playerBucksData
	local playerXPData
	
	pcall(function()
		playerBucksData = bucksDataStores:GetAsync(player.UserId.."-Bucks") --example: 12323425-Bucks
		playerXPData = xpDataStores:GetAsync(player.UserId.."XP")
	end)
	
	if playerBucksData ~= nil then
		-- Player has saved data, load it in
		bucks.Value = playerBucksData
	else
		--New player
		bucks.Value = defaultBucks
	end
	
	if playerXPData ~= nil then
		-- Player has saved data, load it in
		xp.Value = playerXPData
	else
		--New player
		xp.Value = defaultXP
	end
	
	
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	
	pcall(function()
		bucksDataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
		print("Bucks Data saved")
		xpDataStores:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
		print("XP data saved")
		bindableEvent:Fire()
	end)
	
end)

1 comment

0

o.o

1 answer

✓ Accepted answer

SIMPLE FIX

On Line 31, the loading string says .."XP" instead of .."-XP"

USING TABLES

If you want to save the values more efficiently, you can add them to a table and save them, rather than using multiple DataStores

You also don't require the local variable defaults since pre-setting them means that if the datastore has data, then it will overwrite the variables, or else it will leave them alone.

REVISED SERVER SCRIPT

local DataStoreTable = game:GetService("DataStoreService"):GetDataStore("StatsDataStore")

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

	local bucks = Instance.new("IntValue")
	bucks.Name = "Bucks"
	bucks.Value = 15
	bucks.Parent = leaderstats

	local xp = Instance.new("IntValue")
	xp.Name = "XP"
	xp.Value = 0
	xp.Parent = leaderstats

	local success, result = pcall(function()
		DataStoreTable:GetAsync(player.UserId)
	end)

	if not success then
		warn(result)
	else
		local LoadArray = DataStoreTable:GetAsync(player.UserId)
		if LoadArray then
			bucks.Value = LoadArray[1]
			xp.Value = LoadArray[2]
		end
	end
end)

local bindableEvent = Instance.new("BindableEvent")

game:GetService("Players").PlayerRemoving:Connect(function(player)
	local SaveArray = {player.leaderstats.Bucks.Value, player.leaderstats.XP.Value}
	DataStoreTable:SetAsync(player.UserId, SaveArray)
	bindableEvent:Fire()
end)

2 comments

0

Thank you so much. While I see your code looks much more professional and organized using tables, are there any benefits to using a table instead of my original method?

0

@alex, no problem. Now, technically, the choice is up to you; however, using a table would allow you to have all of the player's data available to you at once, and yes it certainly makes things much more organized. It would also make sure that if a player if missing data from one stat they are not keeping the data for all other stats since the DataStore wouldn't have loaded properly. The DataStore

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