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

What does attempt to index local 'player' (a nil value) mean?

What does Players.TheNyghtmare.PlayerGui.Shop.Frame.Main.Item1.Script:6: attempt to index local 'player' (a nil value) mean in this situation?

My script is below:

script.Parent.MouseButton1Click:connect(function()
 local RS = game:GetService("ReplicatedStorage")
 local item =  RS:WaitForChild("Sword")
 local price = 250 -- Change Your Price Here
 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")
 
 if stats.Coins.Value == price then -- Change the Money to your Currency Name
  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)

Output error:

01:02:13.152 - Players.TheNyghtmare.PlayerGui.Shop.Frame.Main.Item1.Script:6: attempt to index local 'player' (a nil value) 01:02:13.153 - Stack Begin 01:02:13.155 - Script 'Players.TheNyghtmare.PlayerGui.Shop.Frame.Main.Item1.Script', Line 6 01:02:13.156 - Stack End

2 answers

I am assuming that this is a Script object since game.Players.LocalPlayer is returning nil.

However, that isn't the first problem. Even if you simply change this to a LocalScript, the price will not change and the item will not be cloned. This is due to FilteringEnabled, a property in all Roblox games that stops any client script changes from replicating to the entire server.

This will make it so your cash only visually decreases, and not actually decrease. Think of it like inspect elementing your Robux count. It may look higher, but it definitely is not.

I suggest before fully fixing the issue here, try and learn more about RemoteEvents and how to use them. https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

You tried to get the local player in a server script, instead use:

game.Players.PlayerAdded:Connect(function(player)
print(player.Name)

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