Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
OnaKat 444 Moderation Voter
5 years ago

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)
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! deathmatch7540 7 — 5y
Ad

Answer this question