InvokeClient not yielding

Hi, I am using RemoteFunction:InvokeClient() but the server doesnt wait for the answer of the client and directly sets my variable as nil.
Here’s the code :

– SERVER

						local success, err = pcall(function()
							Answer = Services.RS.Shop.Events.PromptIslandPurchase:InvokeClient(Player, "IslandPurchase", areaToTp, islandPrice)
						end)
						print(Answer)

– CLIENT

game:GetService("ReplicatedStorage").Shop.Events.PromptIslandPurchase.OnClientInvoke = function(action, islandName, price)
	if action == "IslandPurchase" then
		local islandPrompt = script.Parent.IslandPrompt; 
		islandPrompt.MainBackground.PromptText.Text = "Would you like to buy ".. islandName.. " for ".. price.. " Coins?"
		islandPrompt.MainBackground.YesButton.MouseButton1Click:Connect(function()
			islandPrompt.Visible = false
			return "Yes"
		end)
		islandPrompt.MainBackground.NoButton.MouseButton1Click:Connect(function()
			islandPrompt.Visible = false
			return "No"
		end)
		islandPrompt.CloseButton.Close.MouseButton1Click:Connect(function()
			islandPrompt.Visible = false
			return "No"
		end)
		islandPrompt.Visible = true
	end
end

What does it print when you print the returned value?

It prints “nil”. (30 characters)
EDIT : Also, the gui is opening, meaning the client receives the invoke

try removing the pcalls around

Answer = Services.RS.Shop.Events.PromptIslandPurchase:InvokeClient(Player, "IslandPurchase", areaToTp, islandPrice)

and see if it errors

I did already but it’s the same thing.

You’re being returned nil because the client-side function does not contain instructions to return anything, as the return statements are inside of anonymous functions that you are connecting to MouseButton1Click events. However, that issue is dwarfed by the fact that this is a terrible way of scripting your UI.

First off, you are invoking the client. This is dangerous as an exploiter could simply force your client-side script to yield permanently, effectively disabling it.

Second, you are only connecting the buttons to functions once the server-side script runs, and you are connecting them every single time said script runs, which will lead to unintended behaviour and could be a memory leak.

You are validating action == "IslandPurchase" on the client; this should be done on the server, and you are using pcall when it’s not necessary as you can be guaranteed a response.

Finally, you are returning strings. This isn’t necessarily an issue unless you are doing any logic with the return, in which case you’ll have magic strings involved which are dangerous.

At a higher level, your approach to the purchasing interaction is flawed. This is what your code is aiming for:
image

This is what you should be aiming for:
image

I would recommend reading about the Roblox Client-Server Model and Remote Functions and Events. If you have any questions send them my way.

2 Likes

This is not what the developer hub says…


Also, that wasn’t my full server code, I’m adding protection to it. But thanks, I’ll re-code it so it aims like that
image

Fair enough, although you still shouldn’t be using InvokeClient.

Either ways, please mark my reply as solution so future browsing players can benefit from it

1 Like