Need help with datastores

I am trying to make a saving system for an RPG game I’m making (I used a similar data store system on a different one and it worked).

  1. Although I’m not entirely done with my data saving system, there is an issue whenever I try to load the Player’s data with an xpcall function

I tried to fix it in any possible way (I even re-wrote my entire script) and It wont be fixed!!! I think the issue is related to the GetAsync function

Here’s my script below

local PlrService = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Storage = DataStoreService:GetDataStore("StorageForStats")


local function LoadData(Plr)
	local Succes,Data = pcall(function()
		return Storage:GetAsync("User/" .. Plr.UserId)
	end)
	
	if Succes and Data then
		print("Success")
	else
		print("Fail")
	end
   return Succes,Data
end

local function addplayer(Plr: Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Plr
	
	local Attributes = Instance.new("Folder")
	Attributes.Parent = Plr
	Attributes.Name = "Attributes"
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
	Level.Value = 1

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

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

	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength"
	Strength.Parent = Attributes
	Strength.Value = 1

	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Parent = Attributes
	Stamina.Value = 1

	local Luck = Instance.new("IntValue")
	Luck.Name = "Luck"
	Luck.Parent = Attributes
	Luck.Value = 1
	
	local Agility = Instance.new("IntValue")
	Agility.Name = "Agility"
	Agility.Parent = Attributes
	Agility.Value = 1
	
	local Success,Data = LoadData(Plr)
	
	if not Success then
		wait(1)
		Plr:Kick("Your data is not loading")
		return
	end
	
	if Data then
		print("success")
	end
	
end

PlrService.PlayerAdded:Connect(function(Plr)
	print(Plr)
	addplayer(Plr)
end)

What exactly doesn’t work? If you look at the script, then you do not use the data anywhere (do not set values) and do not save the data.

Every datastore related issue, I recommend this:

As I mentioned earlier, I am not entirely finished with the script. However, whenever I run the script I returned a string of “Fail”. I tried the same with an xpcall function with a custom error of “fail” (to keep it short) (and Ik that xpcall is VERY similar to pcall)

I believe the issue is that the Data is returned as “nil”

here is the xpcall function error (if the image can load)

The data is nil because it doesn’t exist and the xpcall function is not being used correctly, it should be used like this:

local Succes, Data = xpcall(function()
	--code
	return value
end, function()
	--code when 1 function get error
	return value
end)

Docs: Lua Globals | Roblox Creator Documentation

1 Like

This helped me with the xpcall error, but the Data is still being interpreted as nil. I followed the exact structutre(?) of your code but if this makes sense, I only want to have a returned value when I use the xpcall function.

my script:

local function LoadData(Plr: Player)
	local Succes, Data = xpcall(function()
		return Storage:GetAsync("User/" .. Plr.UserId)
	end, function()
		warn("Player Data Has failed to Load")
	end)

	if Succes and Data then
		print("Success")
	else
		print("Fail")
	end
   return Succes,Data
end

I also dont know what “Data doesnt exist” means.

Again, the data can be nil if, for example, the player first logged in and does not have his own key (database), so checking whether the data is equal to nil is useless here, it will only be needed when setting the values ​​themselves for the player ( If data is nil then set default values)

if Succes then
	print("Success")
else
	print("Fail")
end

Thanks for helping man, I’ll just finish my script (saving data) to see if any other errors pop up, but apart from that, this may be my solution. Really though, thank you

1 Like

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