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

Attempt to index local 'player' a nil value?

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

✓ Accepted answer

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

2 comments

0

Most of the time this error is generated because the Asker is using a Server Script rather than a Local Script for LocalPlayer.

0

Good point, didn't consider he could be using the wrong script type.

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