Leaderstat Value not Subtracting

When a player buys a tool from gui, it subtracts their coins from leaderstats.
The problem is that when I subtract coins from the server side, the value still doesn’t go down.
I tried to debug it by subtracting it by 100 and not the amount, I also looked for similar forum post’s but they were either subtracting it from the local server or some other problem.
I’m using a function for the local server and a remote for the server script.

Local Server:

local function handleCoins(amount, request)
	subtractCoins:InvokeServer(amount, request)
end 

Server:

coinHandler.OnServerInvoke = function(player, amount, request)
	local leaderstats = player:WaitForChild("leaderstats")
	local coins = leaderstats:WaitForChild("Coins").Value
	
	if request == "Subtract" then
		coins = coins - amount
	end
	
	if request == "Add" then
		coins = coins + amount
	end
end

This line creates a local variable coins that stores the value of Coins. Any changes you make to coins only affect this local variable, not the actual Coins object in leaderstats.

You’re supposed to set Value of the numbervalue to the coins variable.

leaderstats:WaitForChild("Coins").Value = coin - amount

I also suggest reading the Security Tactics and Cheat Mitigation | Documentation - Roblox Creator Hub docs. As exploiters can pass in negative numbers in this RemoteFunction to gain infinite coins.

1 Like

@SyntaxMenace Already solved it, but another thing is that, as long as i know the functions InvokeServer() and OnServerInvoke are for Remote Functions, so if you are using a RemoteEvent, instead use FireServer()
and OnServerEvent

I can be wrong, if i am you are free to correct me.
Have a nice day!

2 Likes

Okay! It does make sense though because It’s written as local.

3 Likes

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