Problem with loading Stats

The data of the player doesn’t load, i tried to put or 0 but now it always loads 0. If i remove it the first time that the player joins the “while wait(0.1)” doesn’t work, but if he rejoins it works (if i removed or).

local Short = require(game.ReplicatedStorage.Short)
local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Level = Instance.new("NumberValue")
	Level.Name = "Level"
	Level.Value = 0
	Level.Parent = leaderstats
	
	local currencies = Instance.new("Folder")
	currencies.Name = "currencies"
	currencies.Parent = player
	
	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Value = 0
	Money.Parent = currencies


	local Multiplier = Instance.new("NumberValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 1
	Multiplier.Parent = currencies

	local Rebirth = Instance.new("NumberValue")
	Rebirth.Name = "Rebirth"
	Rebirth.Value = 0
	Rebirth.Parent = currencies
	
	local UltraRebirth = Instance.new("NumberValue")
	UltraRebirth.Name = "UltraRebirth"
	UltraRebirth.Value = 0
	UltraRebirth.Parent = currencies
	
	local Prestige = Instance.new("NumberValue")
	Prestige.Name = "Prestige"
	Prestige.Value = 0
	Prestige.Parent = currencies
	
	local MegaRebirth = Instance.new("NumberValue")
	MegaRebirth.Name = "MegaRebirth"
	MegaRebirth.Value = 0
	MegaRebirth.Parent = currencies
	
	local GigaRebirth = Instance.new("NumberValue")
	GigaRebirth.Name = "GigaRebirth"
	GigaRebirth.Value = 0
	GigaRebirth.Parent = currencies
	
	local UltraPrestige = Instance.new("NumberValue")
	UltraPrestige.Name = "UltraPrestige"
	UltraPrestige.Value = 0
	UltraPrestige.Parent = currencies
	
	local playerUserId = "Player_"..player.UserId

	-- loads DATA
	local data
	local success, errormessage = pcall(function()
		data = mydatastore:GetAsync(playerUserId)	
	end)


	if success then
		Money.Value = data.Money or 0
		Multiplier.Value = data.Multiplier or 1
		Rebirth.Value = data.Rebirth or 0
		UltraRebirth.Value = data.UltraRebirth or 0
		Prestige.Value = data.Prestige or 0
		MegaRebirth.Value = data.MegaRebirth or 0
		GigaRebirth.Value = data.GigaRebirth or 0
		UltraPrestige.Value = data.UltraPrestige or 0
		Level.Value = data.Level or 0
	end
	
	while wait(0.1) do	
		Money.Value = Money.Value + (1*Multiplier.Value) -- add *reb.Value and *ult.Value and *pre.Value if u want rebirths and ultra rebirths and prestiges to multiply cash earnings
		Level.Value = ((math.log10(Money.Value+1)*0.001)+((math.log10(Multiplier.Value)*0.01))+((math.log10(Rebirth.Value+1)*0.1))+((math.log10(UltraRebirth.Value+1)*1))+((math.log10(Prestige.Value+1)*10))+((math.log10(MegaRebirth.Value+1)*100))+((math.log10(GigaRebirth.Value+1)*1000))+((math.log10(UltraPrestige.Value+1)*10000)))
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId

	local data = {
		Money = player.currencies.Money.Value;
		Multiplier = player.currencies.Multiplier.Value;
		Rebirth = player.currencies.Rebirth.Value;
		UltraRebirth = player.currencies.UltraRebirth.Value;
		Prestige = player.currencies.Prestige.Value;
		MegaRebirth = player.currencies.MegaRebirth.Value;
		GigaRebirth = player.currencies.GigaRebirth.Value;
		UltraPrestige = player.currencies.UltraPrestige.Value;
		Level = player.leaderstats.Level.Value
	}

	local success, errormessage = pcall(function()
		mydatastore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Data succesfully saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
end)
1 Like

idk how to explain this but pcalling GetAsync datastore, you need to get what the datastore value gives so i think this should hopefully work

local data
local success,result = pcall(function()
  return mydatastore:GetAsync(playerUserId) -- return as in it gives what the datastore value holds so this is the 'result'
end)
data = result
if success then
--continue on
1 Like

but still there is the bug that i don’t gain money at the first join ever in the game.

EDIT: i think the problem is somewhere in while wait(0.1)

	while wait(0.1) do	
		Money.Value = Money.Value + (1*Multiplier.Value) 
		Level.Value = ((math.log10(Money.Value+1)*0.001)+((math.log10(Multiplier.Value)*0.01))+((math.log10(Rebirth.Value+1)*0.1))+((math.log10(UltraRebirth.Value+1)*1))+((math.log10(Prestige.Value+1)*10))+((math.log10(MegaRebirth.Value+1)*100))+((math.log10(GigaRebirth.Value+1)*1000))+((math.log10(UltraPrestige.Value+1)*10000)))
	end

SECOND EDIT:
The first time i have the stats but i don’t gain the money. But if i rejoin it gives.

local playerUserId = "Player_"..player.UserId
1 Like

Shouldn’t you be saving NumberValues as IntValues?

i short them into: M, B, T, Qd, Qn, Sx…

Did you try to put fake data to see if it saves and loads properly?(through datastore editor)

You don’t need to put or 1, 0 since the Data already knows if it can’t load in then it will set it back to the default value

If there isn’t any data assigned to the datastore key, the data variable will be nil, resulting in an error when you index any key inside the data table. Therefore its needed, or the Op can set a default table if the loaded data is nil.

Fixed by changing how to save!