RemoteFunction really doesnt feel like firing

Am a beginner dev don’t judge, anyway I tried to make a system where if a UI button is clicked it will fire a RemoteFunction which should add to the player’s Cash value in leaderstats but the button just doesnt do anything. Will try Knit if all else fails :stuck_out_tongue:

--Server code
local function GiveCash(player, amount)
	print("player "..player.Name.." is getting cash via remotefunc")
	local ls = player.leaderstats
	local cash = ls.Cash
	
	local yes, no = pcall(function()
		if amount then
			cash.Value = cash.Value + amount
		end
	end)
	
	if no then
		warn("giving cash failed, player "..player.Name.." ,reason "..no)
	end	
	
	if yes then
		print("gave cash to "..player.Name.." ("..player.UserId..") via button remoteevent")
	end	
end

game.ReplicatedStorage.GiveCash.OnServerInvoke = GiveCash```


--Client code
script.Parent.CashButton.Activated:Connect(function()
	game.ReplicatedStorage.GiveCash:InvokeServer(game.Players.LocalPlayer, 20)
end)
2 Likes

RemoteFunctions are supposed to return a value. Try RemoteEvents

3 Likes

Nope, button is still dead. Code is similar tho. Tried Knit, didnt work too.

--server
game.ReplicatedStorage.GiveCash.OnServerEvent:Connect(function(player, amount)
	print("player "..player.Name.." is getting cash via remotefunc")
	local ls = player.leaderstats
	local cash = ls.Cash

	local yes, no = pcall(function()
		if amount then
			cash.Value = cash.Value + amount
		end
	end)

	if no then
		warn("giving cash failed, player "..player.Name.." ,reason "..no)
	end	

	if yes then
		print("gave cash to "..player.Name.." ("..player.UserId..") via button remoteevent")
	end
end)
--client
script.Parent.CashButton.Activated:Connect(function()
	game.ReplicatedStorage.GiveCash:FireServer(game.Players.LocalPlayer, 20)
end)```
1 Like

Maybe use MouseButton1Click instead of Activated.

1 Like

Add a print statement in the client code to see if it fires there:

--client
script.Parent.CashButton.Activated:Connect(function()
	print("Fire")
	game.ReplicatedStorage.GiveCash:FireServer(game.Players.LocalPlayer, 20)
end)
1 Like

Nope, didn’t work. Had the same issue with GUI buttons(and prox. prompts) not working before

1 Like

Yup, theres the issue, the GUI button doesnt get activated.

1 Like

Then do as @SomeFedoraGuy said:

script.Parent.CashButton.MouseButton1Click:Connect(function()
	print("Fire")
	game.ReplicatedStorage.GiveCash:FireServer(game.Players.LocalPlayer, 20)
end)
1 Like

Tried that too, MouseButton1Click doesnt work either

1 Like

Any errors in the output?
Can you show your button’s location relative to the localscript’s location in the explorer?

1 Like

Thats the main problem - there is no output. Script is UIHandler, they have the same parent in the hierarchy.
image

1 Like

Are you sure there’s nothing in the output?

1 Like

Yes - the message in console is a debug message for a datastore, but nothing happens

1 Like

Try MouseButton1Down or MouseButton1Up

1 Like

Are you sure you’re clicking the correct button?
Highlight it in the explorer to see where the button is located on the screen.

Aditionally I also think there’s an ‘Active’ property on buttons. Make sure that’s enabled so clicks can be detected.

1 Like

Its because the client code is sending the player, this automatically gets filled in by roblox.

Try game.ReplicatedStorage.GiveCash:InvokeServer(20)

Also I would like to alert you that this will allow exploiters to give themself as much money as they want by doing:

game.ReplicatedStorage.GiveCash:InvokeServer(math.huge)

which will give them infinite, also you should use a remotevent for this.
Also apply some sanity cheks to this, this will still allow them to get infinite cash but will supply some checks to erroring the code, the client should not be in charge of how much cash it gets:

Code
--> Server Script, make GiveCash a RemoteEvent.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Cache = {} --> Ratelimit them

ReplicatedStorage.GiveCash:OnServerEvent:Connect(function(Player, Amount)
    if type(Amount) ~= "number" then
        return -- Detects Passing a Non Number
    end

    if Amount ~= Amount then
         return -- Detects Passing NaN
    end

    if Cache[Player] and os.clock() - Cache[Player] < 0.5 the
           return --> If it gets fired one at a time in less then 0.5 seconds it will ignore it, this stops spamming it
    end

    Cache[Player] = os.clock()
    Player.leaderstats.Cash.Value += Amount
end)
--> Client Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.GiveCash:FireServer(20)
1 Like

Tried that script, problem is that the button does not want to trigger. Tried a prox. prompt and a click detector - none of them work.

1 Like

Could you show me the code for the button?

1 Like

Sure! Heres the code for both the UI button and the other two

script.Parent.CashButton.MouseButton1Down:Connect(function()
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	ReplicatedStorage.GiveCash:FireServer(20)
	print("FIRED LMAO")
end)
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	ReplicatedStorage.GiveCash:FireServer(20)	
end)

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	ReplicatedStorage.GiveCash:FireServer(20)
end)

Is the button located in the users playergui?
can you send me a picture of the explorer?