"Attempt to index nil with "Upgrades", but it shouldnt index nil with it

Hello, im trying to make an upgrade system in which when you buy a certain thing it increases your currency gain by 1 (currency.Value = currency.Value + 1 + upgradesValue), but it gives me the error “Attempt to index nil with ‘Upgrades’”. I will add my leaderstats code and the button code that is supposed to increase the “Upgrades” value by 1:
Leaderstats:

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
	
local Points = Instance.new("IntValue")
Points.Name = "Points"
Points.Parent = leaderstats
	
local Upgrades = Instance.new("IntValue")
Upgrades.Name = "Upgrades"
Upgrades.Parent = player

The button code:

script.Parent.MouseButton1Click:Connect(function(player)
	player.Upgrades.Value = player.Upgrades.Value + 1
end)

The button code is a server script, and ive tested this exact button script with a clickdetector and it works fine, however in the text button it doesnt work. What did i do wrong?

1 Like

You are trying to index Upgrades as a direct child of the player.
As far as I can understand your code, you’d probably want to access Upgrades inside of leaderstats.

Something like this should do:

player.leaderstats.Upgrades.Value += 1

The MouseButton1Click event doesn’t pass any information into the callback function. Buttons should probably be scripted in LocalScripts and therefore you could access

local player = game.Players.LocalPlayer

Your button code should look like

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function() -- remove player argument
	player.Upgrades.Value = player.Upgrades.Value + 1
end)

it’s also worth noting that by handling any kind of currency on the client is dangerous if you don’t want players to be able to exploit their currency easily. I’d recommend firing the server though a RemoteEvent and handling

player.Upgrades.Value = player.Upgrades.Value + 1

on the server.

1 Like

Is your script that creates the “Upgrades” value a server or local script? It needs to be a server script.

1 Like