Problem with giving cash for the first time joining the game

  1. What do you want to achieve? I want to achieve that you don’t need to rejoin to gain cash every 0.1 seconds

  2. What is the issue? The issue is that if you join the first time in the game it doesn’t give the money but you have the multiplier that grants you the money, if you rejoin it gives the money

Script:

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) 
		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)

Any errors? If so, which line?

ServerScriptService.Leaderstats:70: attempt to index nil with “Money”

Where though? I don’t know where line 70 is.

	Money.Value = data.Money or 0

when loads data

It’s likely due to you stating Money rather than data.Money, or data.Multiplier, etc. Try that and see if it works.

I believe you have created like 3 threads yet for this same problem, why not just use the original thread for it instead of creating new ones? Anyways, you’re kind of using the pcall in a different way, what I would do is:

local success, response = pcall(mydatastore.GetAsync, mydatastore, playerUserId)
local data = response or {}

if success then
 --LoadStuff
else
   print(response) --Error message received as response
end

The thing with your current code seems to be that, when the player is new, the data table will be nil and you’re trying to get the “Money” key inside the data variable that is nil. So the error.

The data which loads is nil, you cannot index in a nil value.

You just need to check if data is nil, if it is, set the values to the default and if it isn’t, set them to the saved ones.

	if success then
        if data 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
   else
        Money.Value = 0
		Multiplier.Value =  1
		Rebirth.Value =  0
		UltraRebirth.Value = 0
		Prestige.Value = 0
		MegaRebirth.Value = 0
		GigaRebirth.Value =  0
		UltraPrestige.Value = 0
		Level.Value = 0
        end
	end

Sorry for bad format.

1 Like

Soo the multiplier is “1” but it doesn’t change the money value

EDIT: The while wait(0.1) do doesn’t work

i send you screenshot

The multiplier supposes to give money every 0.1 seconds but don’t.

EDIT: The problem is to gain the cash, but if i rejoin it gives the money

Fixed! I changed way to Save! asdfasdf