My gifting/donating currency isn't working

Hello all.
I was hoping someone could help me find a solution for my gifting system.
Output:
Screen Shot 2022-07-22 at 4.24.18 PM

Local Script:

local Player = game.Players.LocalPlayer
local StudsValue = Player:FindFirstChild("leaderstats").Studs
local GiftFrame = script.Parent
local AmountBox = GiftFrame.AmountBox
local SendButton = GiftFrame.Send
local UsernameBox = GiftFrame.UsernameBox

SendButton.MouseButton1Down:Connect(function()
	local Amount = tonumber(AmountBox.Text)
	local User = UsernameBox.Text
	
	if StudsValue.Value >= Amount and game.Players:FindFirstChild(User) then
		local newamount = Amount
		game.ReplicatedStorage.GiftingStuds:InvokeServer(Player, User, newamount)
		--StudsBalue.Value -= Amount
		--game.Players:FindFirstChild(User):FindFirstChild("leaderstats").Studs.Value += Amount
		SendButton.Text = "Sent"
		task.wait(1)
		SendButton.Text = "Send"
	else
		SendButton.Text = "Something went wrong."
		task.wait(1.5)
		SendButton.Text = "Send"
	end
end)

game:GetService("ScriptContext").Error:Connect(function(msg, _, script)
	SendButton.Text = "Something went wrong."
	task.wait(1)
	SendButton.Text = "Send"
end)

Server Script:

game.ReplicatedStorage.GiftingStuds.OnServerInvoke = function(Gifter, Reciever, Amount)
	Gifter:FindFirstChild("leaderstats").Studs.Value -= Amount
	game.Players:FindFirstChild(Reciever):FindFirstChild("leaderstats").Studs.Value += Amount
end

There is a RemoteFunction in Replicated Storage and the currrency is ‘studs’.

Any help is appreciated.
Thanks!
@Tomroblox54321

When you invoke a remote function to the server, it sends the player who invoked the function as an argument, therefore passing an extra argument to the OnInvoke function.

Your server script needs to handle this extra argument.
A simple way to solve your problem is changing the invoke in the local script to:

--[[Line 14]] game.ReplicatedStorage.GiftingStuds:InvokeServer(User, newamount)

This works because the server runs the gifting function with the arguments (Gifter, Reciever, Amount), and the gifter is always the player who fired the function.

As an ex-exploiter, I also can tell you that this solution also has the added bonus of stopping exploiters from spoofing the first value in order to get other people to gift to them by running code similar to this:

-- This code doesn't actually work thank god
for _, player in pairs(game.players:GetPlayers()) do
    if player ~= game.Players.LocalPlayer then
        game.ReplicatedStorage.GiftingStuds:InvokeServer(player, game.Players.LocalPlayer, player.leaderstats.Studs.Value)
    end
end

It worked. Thanks a ton. I haven’t scripted in awhile so I forgot about that with the remotefunctions. I’m glad that you’ve stopped exploiting and that you are helping the community with knowledge of yours.
Thanks!

1 Like