Attempt to index nil with 'leaderstats'

I am trying to check how many rebirths a player has and then if it is more than 0 then the amount of coins they get is multiplied by 1.5 and then however many rebirths that they have. I try doing the same with Exp as well.

But the Script doesn’t work because it says attempt to index nil with ‘leaderstats’.

I don’t know how to fix it.

if player.leaderstats.Rebirth.Value > 0 then
		coins = coins * 1.5 *player.leaderstats.Rebirth.Value
	end
	
	if player.leaderstats.Rebirth.Value > 0 then
		exp = exp * 1.5 *player.leaderstats.Rebirth.Value
	end
	

There is the place where it errors

Thanks for Helping.

Could you show the full code? Do you even have a player variable/parameter initalized?

Is there another script making the leaderstats folder?

local part = script.Parent
local canGet = true
local player = game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local DoubleCoingamePassID = 16457854
local DoubleXPgamePassID = 16475164
local TripleXPgamePassID = 16477514
local TripleCoingamePassID = 16477217
local ReplicatedStorage = game:GetService("ReplicatedStorage")



local deb = false

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not plr or deb then return end

	local lstats = plr.leaderstats
	deb = true
	
	 
	local coins = 10
	local exp = 15

	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,DoubleCoingamePassID) then
		coins *= 2
	end

	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,DoubleXPgamePassID) then
		exp *= 2
	end

	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,TripleCoingamePassID) then
		coins *= 3
	end

	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,TripleXPgamePassID) then
		exp *= 3
	end
	
	if player.leaderstats.Rebirth.Value > 0 then
		coins = coins * 1.5 *player.leaderstats.Rebirth.Value
	end
	
	if player.leaderstats.Rebirth.Value > 0 then
		exp = exp * 1.5 *player.leaderstats.Rebirth.Value
	end
	

	lstats.Coins.Value += coins
	lstats.Exp.Value += exp
	wait(1)
	deb = false
end)

Change player to plr, you made a plr variable, not a player variable

Yes I have the leaderstats in a different script but this is the one that gives the Coins and Exp

But now it says Maximum event re-entrancy depth exceeded for NumberValue.Changed

Show the code you have in otehr scripts that show the changed event, you’re probably creating an infinite loop in a Changed event

xpz.Changed:connect(function()
		if player.leaderstats:WaitForChild("Exp").Value >= player.notleader:WaitForChild("ExpNeeded").Value then

			levelz.Value += 1

			xpz.Value -= xpn.Value
			
			xpn.Value = math.floor(xpn.Value * 1)
		end
		

It probably has something to do with that

Hmmm not sure then sorry

[3O Characters]

That could be related, if you don’t hae a changed event set up for xpn, you could try this

xpz.Changed:connect(function()
	if player.leaderstats:WaitForChild("Exp").Value >= player.notleader:WaitForChild("ExpNeeded").Value then

		levelz.Value += 1

		local needed = xpn.Value
	
		xpn.Value = math.floor(xpn.Value * 1)
		xpz.Value -= needed
	end
	

Also wait, why are you multiplying by 1? It’s the same thing, you need to increase that 1 a swell

That was for testing purposes for rebirth

After a testing everything works now!

1 Like

Glad that everything works for you now! If you have anymore issues don’t be afraid to make another post!

1 Like