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

My GUI shop has a nil value error?

I am creating a small restaurant in Roblox, and was working on the GUI to purchase your food. My original code had quite a few errors so I decided to watch some tutorials to see what I missed. I ended up with the following code:

script.Parent.MouseButton1Click:connect(function()
 local RS = game:GetService("ReplicatedStorage")
 local item =  RS:WaitForChild("FishKabob")
 local price = 5 -- Change Your Price Here
 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")
print("good day")
 
 if stats.Coins.Value >= price then -- Change the Money to your Currency Name
	print("hello")
  stats.Coins.Value = stats.Coins.Value - price
  local cloned = item:Clone()
  local cloned2 = item:Clone()
  cloned2.Parent = player.Backpack
  cloned.Parent = player.StarterGear
 end
end)

Please ignore the prints, I was trying to figure out how far the code went. The prints never showed up, but instead I got this error message.

attempt to index local 'player' (a nil value)

Could anyone help me fix this code so when I click on my GUI Image Button, it gives the kabob and removes 5 coins from the player's leaderstats/currency?

1 answer

Never mind I figured out the problem on my own!

For those with the same issue - make sure that it is in a local script! Regular scripts will not return which player clicked it and will not register game.players.localplayer! Glad I figured it out, hope I can help others.

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