Attempt to index nil with 'leaderstats'



Please help!!!

2 Likes

This isn’t related to the leaderstats, it’s related to the player. In one of your scripts, specifically the workspace.Camero.DriveSeat.Script script, on line 14, the player variable is nil.

By taking a look at your driving script, I was able to fix a few noticeable logical errors, it should work as expected now:

local driver = nil 
local MetersDriven = script:WaitForChild("MetersDriven")
local Seat: Seat = script.Parent

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = Seat.Occupant
	if not humanoid then driver = nil return end
	driver = game.Players:GetPlayerFromCharacter(humanoid.Parent)
end)

while task.wait() do
	MetersDriven.Value += Seat.AssemblyLinearVelocity.Magnitude
	--The values 1689, 0, and 10 may not be accurate, it was hard to read the image
	if MetersDriven.Value <= 1689 or not driver then continue end
	MetersDriven.Value = 0
	driver.leaderstats.Exp.Value += 10
end
4 Likes

i kinda dont understand soo when driver = nil , during loop it will nil.leaderstats… += 10 ?

3 Likes

No, it will run the continue statement, this means it will skip the current loop iteration, which means that when it runs the code below it won’t run for a single iteration. So basically if driver is nil, continue runs, and driver.leaderstats can’t run so it won’t error.

3 Likes

understand so i assume it will stop loop once theres no player?

3 Likes

No, it will skip the current iteration, not stop the loop entirely. Here’s an example of what I mean:

for i = 1, 100 do
	if i%2 == 0 then continue end
	print(i)
end

This only prints the odd numbers because the continue makes it skip the iterations where i is even.

2 Likes

Thanks so much! Have a nice day