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