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

Attempt to index nil with leaderstats? Please help me!

Asked by 3 years ago
Edited 3 years ago

I'm working a simulator game and this is a local script inside a textbutton. It's a upgrade gui for the simulator. The error is on line 4 (the error is: attempt to index nil with 'leaderstats'). Idk how to fix it so I'm asking the scriptinghelpers' community!

Thanks, notreallysupernova

local CarRespawn = game.ReplicatedStorage.Stats.CarRespawn

script.Parent.MouseButton1Click:connect(function(Player)
    local stats = Player.leaderstats -- This is the error
    local money = stats:WaitForChild("Money")

    local price = 300
    if money.Value >= price then
        money.Value = money.Value - price
        print("Purchased Upgrade")
        CarRespawn.Value = CarRespawn.Value - 2
        end
end)

4 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Your Player value is nil. Which means, "Player" is nil. If it's a local script in startergui, do

local Player = script:FindFirstAncestorOfClass("Player"):WaitForChild("leaderstats",7)

Or

local Player = game.Players.LocalPlayer:WaitForChild("leaderstats",7)

If it's in a server script/normal script, you can't get player by MB1Down in a gui.

Also also, you can't change player leaderstats value in a local script. You have to use a normal script and a remote event for that. You can do this:

--[[ First, put a RemoteEvent in Replicated Storage before doing any of this.
]]
-- Client/Local Script:
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

guiButton.MouseButton1Down:Connect(function()
    remoteEvent:FireServer()
end
-- Server/Normal Script:
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

local CarRespawn = game:GetService("ReplicatedStorage"):WaitForChild("Stats"):WaitForChild("CarRespawn")

local player = remoteEvent.OnServerEvent:Wait()
local leaderstats = player:WaitForChild("leaderstats",7)
local Money = leaderstats:WaitForChild("Money")

local price = 300
if Money.Value >= price then
    Money.Value -= price
    print("Purchased")
    CarRespawn.Value -= 2
end
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I would suggest trying ;

local stats = game.Player.LocalPlayer.leaderstats

Tell me if that worked or not - Maren

Log in to vote
0
Answered by
TheePBHST 154
3 years ago

Because this is a UI, in this case MouseButton1Down doesn’t have “player” as a parameter. So to get the player, you would use “LocalPlayer” which you can get by doing

local player = game.Players.LocalPlayer

That’ll work for you and you don’t need “player” in the parentheses in the MouseButton1Down event.

Log in to vote
0
Answered by 3 years ago

Your error is that you cannot call "Player" in localscripts. Instead, do this:

local CarRespawn = game.ReplicatedStorage.Stats.CarRespawn

script.Parent.MouseButton1Click:connect(function() -- It would not have player here
    local stats = game.Players.LocalPlayer.leaderstats -- This is the error -- use Localplayer instead
    local money = stats:WaitForChild("Money")

    local price = 300
    if money.Value >= price then
        money.Value = money.Value - price
        print("Purchased Upgrade")
        CarRespawn.Value = CarRespawn.Value - 2
        end
end)

Answer this question