Gems Don't Deduct When RemoteEvent Fired

I have a purchase script that’s written inside of a LocalScript, gemCost is a variable defined within the LocalScript and is the amount of gems that should be deducted from the player who fires the RemoteFunction.

A segment of the LocalScript is here:

local function buyItem()
	local player = Players.LocalPlayer
	local playerGui = player.PlayerGui
	clickSound:Play()
	script.Parent.Parent.Parent.Visible = false

	local confirmationUI = playerGui["User Interface"].UserUI.SwordConfirmationUI
	confirmationUI.SwordName.Text = sword_to_give

	if confirmationUI then
		confirmationUI.Visible = true
		confirmationUI.GemsCost.Text = gemCost
		confirmationUI.SwordImage.Image = script.Parent.ImageLabel.Image
		game.Lighting.Blur.Enabled = true

		local yesButton = confirmationUI:FindFirstChild("YesButton")
		if yesButton then
			yesButtonConnection = yesButton.MouseButton1Click:Connect(function()
				if player.leaderstats.Gems.Value >= gemCost then
					confirmationUI.Visible = false
					equipButton.Visible = true
					swordPurchasedEvent:FireServer(gemCost) 

Server-Sided Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swordPurchasedEvent = ReplicatedStorage.RemoteFunctions:WaitForChild("SwordPurchased")

swordPurchasedEvent.OnServerEvent:Connect(function(player, gemCost)
	player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - gemCost
end)

I can confirm that the gemCost variable is properly setup with the correct number, and can confirm that their are no output errors. However, when the function is called and the yesButton is pressed the gems do not get deducted from the player however everything else works fine. Any help or advice would be appreciated, thanks. Please note that I’m new to scripting/RemoteFunctions! :happy3:

try instead

swordPurchasedEvent.OnServerEvent:Connect(function(player, gemCost)
	player.leaderstats.Gems.Value  -= gemCost
end)

Didn’t seem to work, any other suggestions?

Have you checked if the function on the server actually runs? You can do so with a print statement or a breakpoint. If your server side function does not get invoked at all, check that both scripts are referencing the same event. Also as a sidenote, never under any circumstances pass stuff like the cost of an item through a remote event and blindly trust that information on the server. Exploiters can invoke remote events with their own values and for example send a gemCost of -1000 through your remote event, which would actually give them money.

I see, is there any other additional steps I should take or alternative routes to ensure that the gem cost of the item is actually deducted and the deduction is recognized by the server safely?

A good rule of thumb is to never trust the client. In your situation you should save the prices of items on the server and only take the type of item a player wants to purchase as a parameter in your remote event. Then the server checks if the player has enough money for it and then gives it to the player. You’re basically moving all the vital stuff to the server, only giving the client control over which item they want to purchase. How exactly you prevent exploits is a massive topic that is impossible to cover in one post but there are great guides on how to do so on the Forums.

Also to get back to the original problem, have you found a fix for your original issue?

Yeah, I ended up finding a fix. Just concerned about the exploiting factor of it now lol.

It’s probably a good idea to write a quick message about how you fixed your problem and to mark it as the solution.

Regarding exploits, don’t be too worried about them. As long as you roughly follow the rule above you should be fine 99% of the time. If you’re ever in doubt a quick google search will probably help you, or you can always create another post on the forums.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swordPurchasedEvent = ReplicatedStorage.RemoteFunctions.SwordPurchasedEvent

swordPurchasedEvent.OnServerEvent:Connect(function(player, gemCost)
	if player and player:FindFirstChild("leaderstats") then
		local gems = player.leaderstats:FindFirstChild("Gems")
		if gems and gems.Value >= gemCost then
			gems.Value = gems.Value - gemCost
		end
	end
end)

Changed the script to this and it ended up working. :happy3:

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