Problem with giving cash

When i join the game for the first time it doesn’t give the cash and it says that i have a multiplier but when i rejoin it gives the cash. I don’t understand the problem!

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
		Multiplier.Value = data.Multiplier
		Rebirth.Value = data.Rebirth
		UltraRebirth.Value = data.UltraRebirth
		Prestige.Value = data.Prestige
		MegaRebirth.Value = data.MegaRebirth
		GigaRebirth.Value = data.GigaRebirth
		UltraPrestige.Value = data.UltraPrestige
		Level.Value = data.Level
	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

When you join a game for the first time you have no data in the datastores. But i might be getting the wrong thing from the post. Sorry if i am

But when i join for the first time i can see that i have a multiplier

That I would not know the answer to as i never deal with multipliers

It’s not a built-in feature of roblox. It’s a value in my currencies folder.

You’re seeing the value because you’re setting the value of the Multiplier value to 1 before data loads. But because theres no data for first time in the data table that you have defined (as it can be a new player), you need to assign default values. Something like this can work too:

if success then
		Money.Value = data.Money or 500
		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 1
	end