Attempt to perform arithmetic (sub) on number and nil

I’ll try it now, I’ll have to change a couple of things.

Although @JarodOfOrbiter also told the same but @Doom_Vickstar999 says it’s gets errored

Ok, I’m back, it did not work, I have a different idea though that may work, just inserting a NumberValue with the price somewhere, getting it from there, and maybe it works.

Try this -

AddTrails.OnServerEvent:Connect(function(addTrails)

And set the addTrails function to this -

function addTrails(plr, gems)
	local currency = player:WaitForChild("Currency"):WaitForChild("Gems")
	
	currency.Value -= tonumber(gems)
end

Thanks!

I have no idea what you're talking about. I don't see anything wrong with the code. I can't answer this question, because I can't replicate the issue. Can you post a link to the place where you got the code?

Sorry for the late reply, I was on holiday. The code was from a youtube video by Brickman. It's not a big issue, I just wanted to know why it was doing that.

```local function AddTrail(player, gems) player.leaderstats.Trails.Value = player.leaderstats.Trails.Value + gems player.PlayerGui.MainGui.Gems.Text = player.leaderstats.Trails.Value end

AddTrail.OnServerEvent:Connect(function(player, gems)
local player = game.Players:GetPlayerFromCharacter(player.Parent)
print(gems)
AddTrail(player, gems)
end)





I believe your problem is stemming from the fact that you have
```local player = game.Players:GetPlayerFromCharacter(player.Parent)

You are trying to get the player from a character, but the parameter passed to this function is a player, not a character. I believe you want to change this line to

This should fix your problem, but I would recommend that you change the name of the parameter that you pass in to something else, like playerCharacter, because you have a local variable in that scope named player as well.

Tried it, it doesn’t work sadly.

I can’t really, I don’t own the place so I am not allowed to provide permissions. I am not having any trouble with the player though…

But according to it, it should work.

Ok wait, lemme correct my script.

Now try it and check if it work, it should work. I updated my script.

Once again, it doesn’t work, I’m gonna try doing the NumberValue thing I said above.

It would have worked though, Roblox Documentation also recommended what I did. But ok, hope the thing you will do should work.

I didn’t read through the other 52 replies but this should solve your problem, I added a bunch of comments explaining what I changed and why.

-- replaced MouseButton1Click with Activated, so it also works on mobile
button.Activated:Connect(function()
    local gems = 50

    -- removed the unnecessary tonumber(), because gems is already a number
    AddTrail:FireServer(gems)
end)
-- removed the local function, since it didn't do anything but call addTrails()
AddTrails.OnServerEvent:Connect(addTrails)
-- added player to the parameters, so it can be directly connected to the server event
-- also because in the function you use player, but that means you're probably doing something wrong because this is a server script (OnServerEvent)
function addTrails(player, gems)
    -- added a check to see if the player is sending a number
    -- if they aren't this server event will be ignored, and a warning will be printed
    if type(gems) ~= "number" then
        warn(("%s tried to addTrails '%s' which isn't a number"):format(player.Name, tostring(gems)))
        return
    end

	local currency = player:WaitForChild("Currency"):WaitForChild("Gems")
	
    -- removed the unnecessary tonumber(), because gems was already a number and now we have already checked whether gems is a number
	currency.Value -= gems
end

P.S. you shouldn’t allow the client to choose how many gems to add, this can be exploited by hackers.

I get the warning sadly :frowning:

Could you show the code you used?

Oh ok, here it is:

AddTrails.OnServerEvent:Connect(addTrails)
function addTrails(player, gems)
	if type(gems) ~= "number" then
		warn(("%s tried to addTrails '%s' which isn't a number"):format(player.Name, tostring(gems)))
		return
	end
	
	local currency = player:WaitForChild("Currency"):WaitForChild("Gems")

	currency.Value -= gems
end

Edit: Here is the local one too, I forgot to paste it:

button.Activated:Connect(function()
	local gems = 50
	
	AddTrail:FireServer(gems)
end)

Here you have AddTrails

But here it’s AddTrail?

Is it the same event?

the first argument is always the player that fired the event. You’re currently doing number-playerinstance

Yes it is the same event, I just named them differently mistakenly

Could you temporarily change the addTrails() function to something like this?

function addTrails(...)
	print{...}
end

And then post what it prints?