Cannot Compare UserData with Number Error; What's Going On?

Scripting Support.

CheckPurchaseUserdataError.rbxl (18.8 KB)

Hello all! I recently became a member of the Roblox Developer Forum so I might not know all the steps I need to take in order to make debugging my code convenient for you all.

I recently ran into a really interesting problem in my game.

I was creating a shop menu, and I wanted to check whether or not the player has enough “Coins” in their leaderstats to purchase the said amount. I noticed that if someone was exploiting and changed the number of their coins locally, the transaction would go through the system. To eliminate the problem, I have decided to create a remote function under Replicated Storage that would check to see whether the player has enough coins (on the server-side), and would return true if they do.

In the Server Script…

--In a server script under ServerScriptService

--Check to see whether it is possible for the user to purchase the item )
--(on the server-side to prevent exploiting)
--"fee" is the amount required to purchase Item

checkEligibility.OnServerInvoke = function(player, fee)
	local coins = player:WaitForChild("leaderstats"):WaitForChild("Coins")
	
	print(coins.Value) --IT PRINTS THE CORRECT AMOUNT BUT WHY CANT IT COMPAIR!?!?!?!
	
	if coins.Value >= fee then --error is here
		return true
	else
		return false
	end
end

And in the Client Script…

if checkEligibility:InvokeServer(game.Players.LocalPlayer, price) then
--continue to purchase
else
--continue to do the course of action that will notify the player that they couldn't purchase the item
end

Whenever I invoked this function, I would get an error stating: “16:06:26.033 - ServerScriptService.Script:26: attempt to compare userdata with number”

What exactly is going on, and how do I fix this problem?

1 Like

The Code Review category is for reviewing code that already works, what you want is the Scripting Support category instead.

The issue with the code is that fee is an userdata, probably a Roblox instance. Specifically, you’re invoking InvokeServer with a Player argument for its first argument. InvokeServer already passes the local player to the server, so this is not needed. Your code is trying to compare coins.Value >= your_player.

Also, it’s bad practice to allow your client to tell the server how much money something costs, this is 100% exploitable.

5 Likes

Thank you so much for the advice, I really do appreciate it!