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

Attempt to index local 'player' a nil value?

Asked by 7 years ago

Alright so I made a gun buying GUI for a street dealer and it won't buy the gun or take the money away due to the error aforesaid in the title.

    local player = game:GetService("Players").LocalPlayer
    local money = player:WaitForChild("Money")

function BuyAK()
    if money.Value >= 125 then
        money.Value = money.Value - 125
            local AK47 = game.ServerStorage.AK47:clone()
            AK47.Parent = script.Parent.Parent.Parent.Parent.Parent.Backpack
            script.Parent.Text = "Gun Purchased"
        wait(5)
            script.Parent.Text = "Buy AK-47"
       else
            script.Parent.Text = "Purchase Failed"
        wait(5)
            script.Parent.Text = "Buy AK-47"
    end
  end


script.Parent.MouseButton1Down:connect(BuyAK())

1 answer

Log in to vote
2
Answered by 7 years ago

Right, so what I think is happening is the script is being executed before the LocalPlayer property is loaded. To fix it just wait for LocalPlayer to be occupied, put this at the start of your script:

repeat wait() until game.Players.LocalPlayer~=nil
0
Most of the time this error is generated because the Asker is using a Server Script rather than a Local Script for LocalPlayer. M39a9am3R 3210 — 7y
0
Good point, didn't consider he could be using the wrong script type. IntegerUnderflow 25 — 7y
Ad

Answer this question