Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error Message: Attempt to index local 'leader' (a nil value) won't go away. Help?

Players.deathmatch7540.PlayerGui.PurchaseCar.Yes - $5,000.LocalScript:11: attempt to index local 'leader' (a nil value)

I'm currently trying to make a purchase/spawn button for a vehicle, except the above error message keeps popping up. I tried adding WaitForChild("leaderstats") and other such variations to no success. Help with this would be greatly appreciated!

Deathmatch7540

local button = script.Parent

local function onButtonActivated(player)

local plr = player.name
local leader = plr.leaderstats
local price = script.Parent.Parent.Price.Value
local car = workspace.CarsInGame:FindFirstChild(plr)


if leader.Cash.Value >= price then
	leader.Cash.Value = leader.Cash.Value - price
	if car then
		car:Destroy()
		local clone = game.ReplicatedStorage.Cars.ArmyTruck:clone()
		clone.Parent = game.Workspace.CarsInGame
		clone.Name = plr 
	else
		local clone = game.ReplicatedStorage.Cars.ArmyTruck:clone()
		clone.Parent = game.Workspace.CarsInGame
		clone.Name = plr 
	end
end

script.Parent.Parent:Destroy()

end                            

button.Activated:Connect(onButtonActivated)

1 answer

✓ Accepted answer

That because it can't find the leaderstats.

local plr = player.name
local leader = plr.leaderstats

You put that plr is a string value ( player.Name not player.name )

You can navigate to player with LocalPlayer ( only work with local script )

local leader = game.Players.LocalPlayer:FindFirstChild("leaderstats")

And wait until it can find leaderstats.

repeat wait() until game.Players.LocalPlayer:FindFirstChild("leaderstats")
local leader = game.Players.LocalPlayer.leaderstats

Your script will be like this.

local button = script.Parent

local function onButtonActivated()

local plr = plr.Name
repeat wait() until game.Players.LocalPlayer:FindFirstChild("leaderstats")
local leader = game.Players.LocalPlayer.leaderstats
local price = script.Parent.Parent.Price.Value
local car = workspace.CarsInGame:FindFirstChild(plr)


if leader.Cash.Value >= price then
    leader.Cash.Value = leader.Cash.Value - price
    if car then
        car:Destroy()
        local clone = game.ReplicatedStorage.Cars.ArmyTruck:clone()
        clone.Parent = game.Workspace.CarsInGame
        clone.Name = plr
    else
        local clone = game.ReplicatedStorage.Cars.ArmyTruck:clone()
        clone.Parent = game.Workspace.CarsInGame
        clone.Name = plr 
    end
end

script.Parent.Parent:Destroy()

end                            

button.Activated:Connect(onButtonActivated)

1 comment

0

Alright, thank you! I do believe it worked because I am no longer getting the error message. I just have to fix all the other stuff that I messed with!

This discussion is preserved as it was. New answers, comments, and votes are closed.