Attempt to perform arithmetic (sub) on number and Instance error

This Is the Serverscript

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Amount)
	plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value - (Amount)
end)

it shows with the error attempt to perform arithmetic (sub) on number and Instance

Amount and plr are a variable from the local script

Pls Help

LargeHotDogs13

By looking at the error, it seems “Amount” is an Instance.

1 Like

Amount Is A Int Value
need characaters

1 Like

If amount is an Int Value, you would need to add a .Value after Amount.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Amount)
	plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value - (Amount.Value)
end)

Also, this Remote Event needs some sort of sanity check to prevent exploiters from abusing it.

1 Like
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Amount)
	plr.leaderstats.Gems.Value -= Amount.Value
end)

“-=” is just a shorter and better way rather than this line :
plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value - Amount.Value

And yes, you might want to add some sanity checks here.

1 Like

ive got the script like this now

game.ReplicatedStorage.RemoteEvent:Connect(function(plr, Amount)

plr.leaderstats.Gems.Value = plr.leaderstats.Gems.Value - (Amount.Value)

end)
But this is the error now: Value is not a valid member of Player “Players.LargeHotDogs13”

It seems you are probably passing the player in the FireServer and the value after. Only send the value. The server automatically receives the player, so you don’t need to send it.

1 Like
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Amount)
	plr.leaderstats.Gems.Value -= Amount + 0 -- if value is string
end)

IntValue, (also formally known as Integer Value), would just be an integer. I don’t believe they literally mean an IntValue…

Now onto @LargeHotDogs13, this should work:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr: Player, Amount: number) -- Type checking.
    if not plr then return end -- If the player doesn't exist, stop the script.

    pcall(function()
    	plr.leaderstats.Gems.Value -= Amount or 10
    end)
end)

This essentially will catch all the errors that occur. Change the “10” value to whatever base value it should remove, in the instance that Amount does not exist, so the script won’t break.

Pcall will catch any errors and will make sure the script doesn’t break no matter what. Type checking will also (hopefully) help with errors, as well as checking if the player exists! Hopefully this helps you! :grin:

1 Like
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Amount)
	if Amount.Value then
		Player.leaderstats.Gems.Value -= Amount.Value
	elseif Amount ~= nil then
		Player.leaderstats.Gems.Value -= Amount
	end 
end)