Why is this Remote Function invoked at the start of every Play test, then doesn't work

This remote function has been bothering me lately and I want to know a quick work around to this. I am not sure if this is a Studio/Engine bug, but I am struggling to understand why this is happening.

Context and explanation

  • The main idea is to have a Local script detect a “Player Ready” button press and send the player’s details over to a server script to register the player as ready(and register a few of his details as well).
  • Problem: Immediately as I start the play test, the remote Function gets invoked for no reason, then later becomes irresponsive and stops working(can’t get invoked again)
  • The remote function is in Replicated storage, the local script is in StarterPlayerScripts, while the server script is in the workspace
  • Pointer: This seems to be occurring only with remote functions that are called by scripts which go from client to server

Code

local script(StarterPlayerScripts)

local function onReadyPress(playerNumber, button)
	print("onReadyPress Activated") -- Ready button was pressed
	if playerNumber == 1 and button == ready1Button then -- checks if player clicks on his own ready button
		local readyBool = changeButton(button)
		playerReadyRF:InvokeServer(player.Name, playerNumber, readyBool, TableInstance)
	elseif playerNumber == 2 and button == ready2Button then
		local readyBool = changeButton(button)
		playerReadyRF:InvokeServer(player.Name, playerNumber, readyBool, TableInstance)
	end
end

Server script(workspace[inside a model])

local function onPlayerReady(player, playerName, playerNumber, readyBool, _TableInstance)
	print(player)
	print(playerName)
	print(playerNumber)
	print(_TableInstance)
	print(readyBool)
	if TableInstance == _TableInstance then -- code from here on out registers the player as ready
		if player1.Value == playerName then
			ready1 = readyBool
		elseif player2.Value == playerName then
			ready2 = readyBool	
        end
		print(playerName .. " has ready set to : " .. readyBool)
	end
end
playerReadyRF.OnServerInvoke = onPlayerReady()

Visual representation of the problem
Every time I play test, the remote function gets invoked and later on doesn’t work
RF being invoked immediately

Button being pressed 3 times with no response from RF

What I tried and tested

  • I tried to replicate this in a new place and I got the same results. I basically made a button and had a local script to detect when the button is pressed and then invoke the remote function. The same thing still happened when I pressed play.
  • I even deleted the local script and just remained with the server script code and the same thing still happened.
  • I tried starting Roblox studio without plugins enabled
  • I tried restarting my PC, just incase it was a problem on my end. Same results
  • I tried putting the Server script in ServerScriptService instead of the Workspace.model
  • Another important pointer: The server script looks like it could be the problem, because it seems to only affect RFs which are used by Scripts that use the .OnServerInvoke event.

So I am starting to doubt myself and wonder what to do to find a solution to this. Is it a bug?.. I don’t know. I do feel like I am doing something wrong, so I would appreciate as much help as I could get.

1 Like

on the local script, is there anything that fires the onReadyPress function?

Yes, there is a Mouse1ButtonClick that does this. But if it was fired, I would have known because the function the Mouse1buttonclick is connected to(onReadyPress function) has a print(“onReadyPress”) before it invokes the server.

But to make things even more confusing, I deleted the entire Local script and only remained with the server script… and the same thing still happened! I 'm starting to believe the local script doesn’t have an effect to the problem

Code

ready1Button.MouseButton1Click:Connect(function()
	onReadyPress(playerNumber, ready1Button)
end)
ready2Button.MouseButton1Click:Connect(function()
	onReadyPress(playerNumber, ready2Button)
end)

is there any clone of the code? or something? you should try using the Search tool on the scripts that you suspect of causing the error

Okay, I tried searching for any clones(scripts, code, RFs), but found non.
I do want to point out something, I tried recreating this weird scenario like this.

  1. I created a new place and added a remote function to replicated storage
  2. I created a server script inside Server Script Service
  3. I added the following code to the script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage.RemoteFunction

local function onRemoteFunctionInvoke(player, playerName, userID)
	print("Server Invoked")
	print(playerName)
	print(userID)
end

remoteFunction.OnServerInvoke = onRemoteFunctionInvoke()
  1. Making a local script is optional, but this bug can still happen without it.
  2. Press play and …
    image

dang… so it is that code…

what if here

you try removing the ()

is my last idea (prob wont work)

1 Like

The issue is that when you connect the remote function to the function you call it because you are doing

remoteFunction.OnServerInvoke = onRemoteFunction()

This will call the function as soon as the thread runs (start of play test)

to fix this you would just simply do

remoteFunction.OnServerInvoke = onRemoteFunction

This should fix your isssue

(like the person above stated)

2 Likes

SO I HAD REASON??? NO WAY LOOOOL READ MY LAST REPLY

bruh i cant belive

bruh i responded without reading the replies.

no i mean, i accidentally replied good, i didnt mean to say that you are copying

Oh my gosh, it was that all along :joy:. I actually can’t believe I forgot to remove the brackets

1 Like

Nice, this is a more in depth explanation, I see now.
Thanks for the notice

I would say that Roblox should try to find a little way of not automatically adding brackets at the end of .OnServerInvoke event connects lol.
Thanks guys