RemoteFunction Not Returning Correctly

Hello DevForum,
Today I come to you with a most confusing issue.
Here, you can see a piece of code I have in my Main Menu dedicated to letting the player join the game.
When you initially join, this code runs perfectly fine.
However, when you re-run the code for a second time (albeit a different instance of the main menu), the same code fails to run.

The RemoteEvent and RemoteFunction are sent to the following function:
image

On the first run, the following is printed to the output:
Return! Set Team
Return! Load Character
Loaded Character!
Destroying Blur Effect

On the second run however, only the following is printed to the output:
Return! Set Team
Return! Load Character

This is confusing me because if you look at the print statements, the only thing between “Return! Load Character” and “Loaded Character!” is the return statement. What is causing this script to hang indefinitely? I’m extremely confused. Please help.

It is worth noting that on the second run, the player’s character has been removed using Character:Destroy() and hence the Character property of the player still has a value, but this does not effect the result in my testing.

Additionally, changing RemoteFunction into a RemoteEvent seems to work, but doesn’t have the intended effect (the script must yield until the character has been spawned).

1 Like

Update:
I’m investigating issues with my data storage system. Might be connected to my issue.

Data storage system is unrelated to my issue.
I still seek help!

Hey. Could you send your code in a code block to make it easier to read and look for issues?

This format:

image

Nope; the script is formatted in a way that makes it extremely tedious to format correctly and even if you were to place it in a script, you couldn’t get it to run under the correct circumstances. Besides, it looks like there is only 1 one of interest here.

It’s hard to read it, I don’t understand why you can’t place it on ```
It’s annoying to read like that and hard, copy and paste please

1 Like

As long as it’s indented and formatted properly in the Roblox script editor, you shouldn’t have a problem pasting it over.

Oh, looks like they’ve improved on their code formatting. This is a lot easier now.
Well, I don’t know what you mean by “difficult to read” but here you go, I guess?

local function JoinGame (Button)
	-- Hide Main Menu
	InterfaceManager.HideElement (Screen)
	-- Change Player's Team
	RemoteEvent:FireServer ("Set Team", Teams[Button.Name], true)
	-- Load Player's Character
	local Character = RemoteFunction:InvokeServer ("Get Data", {"Player Data", PlayerID}, true)["Character"]
	for AccessoryName, AccessorySetting in pairs (Character) do
		Character[AccessoryName] = CharacterFrame.AccessoriesList[AccessoryName].Container.Buttons[AccessorySetting].AccessoryID.Value
	end
	RemoteFunction:InvokeServer ("Load Character", Character, true)
	-- Fire Player Joining Event
	PlayerJoiningGame:FireServer ()
    print ("Loaded Character!")
	RemoteEvent:FireServer ("Distribute Information", {PlayerJoiningGame, Player}, false)
	-- Clean-Up
    print ("Destroying Blur Effect")
	Camera.BlurEffect:Destroy ()
	MainMenu:Destroy ()
end

-- Handles Incoming Server Requests
local function HandleRequest (Player, RequestType, Arguments, Yield)
	
	-- Change a Player's Team
	if RequestType == "Set Team" then
		local Team = Arguments
		PlayerSystem.SetTeam (Player, Team)
	end
	-- Load a Player's Character
	if RequestType == "Load Character" then
		local Appearance = Arguments
		PlayerSystem.LoadCharacter (Player, Appearance)
	end
	
	-- Handle Yield Requests
	if Yield then
		print ("Return!", RequestType)
		return
	end
end

Update
Using the following code, I noticed that the return is fired, but not received. The reason I’m lead to this conclusion is because the breakpoint fires on the return, but the function itself doesn’t seem to receive any returned value.

	if Yield then
		print ("Return!", RequestType)
		return "Completed... ".. RequestType
	end

Actually, if you are going to make a main menu, I think you have to make a local script to do that. Try it but with a local script. There is no point on making a remote function if the client can handle it anyways.

What are you on about? The client is sending requests to the server because it has to.

Update
In an attempt to circumnavigate the yielding, I’ve used the following code. This seems to work.
However, this doesn’t resolve my issue. (there are purposes for which this solution cannot be used)

    -- Load Player's Character
	local Character = RemoteFunction:InvokeServer ("Get Data", {"Player Data", PlayerID}, true)["Character"]
	for AccessoryName, AccessorySetting in pairs (Character) do
		Character[AccessoryName] = CharacterFrame.AccessoriesList[AccessoryName].Container.Buttons[AccessorySetting].AccessoryID.Value
	end
	RemoteEvent:FireServer ("Load Character", Character)
	print ("Player Loading...")
	Workspace:WaitForChild (PlayerUsername, 5)
	-- Fire Player Joining Event
	print ("Player Loaded!")