RemoteFunction's server Script's additional code runs without even invoking the server

I am practicing RemoteFunctions and I have a textbutton that fires a RemoteFunction.

It works fine, it returns what it should but the server script’s additional code runs even when I just click test play. I want it to go in effect after I fire the RemoteFunction.

This is the server script:

local Message = game.ReplicatedStorage:WaitForChild(“Message”)

function Message.OnServerInvoke(plr)

local messdel = {
	
"Invoking server..", "Thank you for invoking me "..plr.Name, "Spawned a part in the workspace.", "Created a folder in the workspace."}

print("Putting random color for the player's head.")
plr.Character.Head.BrickColor = BrickColor.Random()

return messdel

The returned value works fine but when I click Test Play it already changes the Head’s color and prints the string. It only does this one time, even after I click the GUI it won’t run anymore. Only the value I return. I thought RemoteFunctions are just the same as RemoteEvents but they return a value. I hope y’all understand me, still working on my explaining skills. I’d be thankful for some help. I can give more info if neccessary about this issue. Gif here

Is your studio settings set where it simulates your game in FilteringEnabled mode?

Yes it is.

So I need to use a function?

RemoteFunction.OnServerInvoke = function(plr)
--do stuff
--do this or the client will be yielding for forever
return true
end)

I’m not sure if the problem is within the server script; I normally don’t handle RemoteFunctions like that (in the way with function Event.OnServerInvoke), but if the script is running fine, there may be a problem with the client itself.

RemoteFunctions do yield until the value is returned when they are called, does the value print in the client and not just in the server alone? If it only prints in the server only, then the script isn’t returning the value, which causes the client’s script to yield indefinitely. which may explain why the GUI breaks.

Good point, I edited my reply. The problem is he declared the invoke incorrectly.

I’m pretty sure you can’t invoke it with your method too, I think you can only do that with RemoteEvents (with ServerEvent of course), not RemoteFunctions.

I invoke RemoteFunctions like such:

EVENT.OnServerInvoke = function(Player, TheScript, TheAction, Variable1, Variable2)
local Chicken = game.Workspace:FindFirstChild("Popeyes"):FindFirstChild("Good_Chicken")
return Chicken
end)

Please put your code in a proper code block so it’s easier to read what exactly you’re trying to do.

```lua
– Your code
```

becomes

-- Your code

I’d like to assume that the issue is that you aren’t writing your code properly.

-- Client-side
local Results = RemoteFunction:InvokeServer()

-- Server-Side
function RemoteFunction.OnServerInvoke(player, args)
    -- stuff
    return whatever
end

Code in a RemoteFunction doesn’t just invoke. There’s definitely something that’s up with your code. Not all relevant code has been provided either; we don’t know what the client is doing.

3 Likes