Clicking not giving me money

I am trying to make it so that when you click you get 1 money, but clicking leads to this error message

script that it leads to:


Manager.Profiles = {}

function Manager.AdjustMony(player: Player, amount: number)
	local profile = Manager.Profiles[player]
	if not profile then return end
	profile.Data.mony = += amount
	player.leaderstats.mony.Value = profile.Data.mony
end

return Manager

line of code that seems to cause it:
profile.Data.mony = += amount

If anyone could help, it’d be greatly appreciated!
(mony is intentionally spelled wrong)

you cant do another equal sign

there is 2 ways to write it correctly

profile.Data.mony  += amount

or

profile.Data.mony  = profile.Data.mony  + amount

try fixing it with removing the first equal sign

I just tried both and still have the same error message

Is that the whole code? If not, please provide the code where you adjust the money.

1 Like

That error explains quite clearly what your problem is: You’re trying to add a number value to a value that doesn’t currently exist (nil). In the script you provided line number 8 is end so that error is located in a different script than the one you provided (or it might not be the whole script like @zahra_y735 suggested)

1 Like

yeah thats the whole thing, i do have other scripts aswell that link to the money adjustment

this is the only other script that handles the money

local ServerScriptService = game:GetService("ServerScriptService")

local Remotes = ReplicatedStorage.Remotes
local PlayerData = require(ServerScriptService.PlayerData.Manager)


local function Click(player: Player)
	PlayerData.AdjustMony(player, 1)
end

Remotes.Click.OnServerEvent:Connect(Click)

I do think that the issue is due to the fact that your module doesn’t seem to be creating a new profile for the player if they don’t already have one, so I’ll see if I can re-write the module to fix that problem

Edit: @cozfl Here’s the new module. I’ve kept the name of the values the same but I changed the profile to work using the player’s UserId so that the data is saved if they exit and re-join the game:

local Manager = {}

Manager.Profiles = {}

function Manager.AdjustMony(player: Player, amount: number)
	local profile = nil

	if Manager.Profiles[player.UserId] then
		profile = Manager.Profiles[player.UserId]
	else
		profile = {Data = {mony = 0}}

		Manager.Profiles[player.UserId] = profile
	end

	profile.Data.mony += amount

	player.leaderstats.mony.Value = profile.Data.mony
end

return Manager

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.