Remote Functions: Script that implemented this callback has been destroyed while calling async callback

I’m working with a local script and a script, trying to do client-server-client communication with a RemoteFunction. What I’m basically doing is a card-game system, where the player clicks cards in their UI, the server processes them, summons the card, and then a function called removeCard() removes (deactivates)the card played in the players hand.

Here is the location of the local script, script, and remotefunction:

--Local Script
local function removeCard(card)
	print("removing...", card)
	card.Active = false
	card.Visible = false
func_play_card.OnClientInvoke = removeCard

local function playCardRequest(plr, card, slot) 
	for i = 1, 2 do
		if table.find(board[i], slot) then 
			local newSlot = table.find(board[i], slot)
			print("pre-server invoke", plr, card, i, newSlot)
			func_play_card:InvokeServer(card, i, newSlot) --data passed: player, team, slot index, card
local function isValidPlay(plr, card, team, slot)
	local cardData
	print("isValidPlay checking", card, team, slot)
	for i, v in ipairs(hand[team]) do
		print(v["HandCard"], card)
		if v["HandCard"] == card then
			cardData = v
			table.remove(hand[team], i)
			print("got cardData", cardData)
	if cardData then
		print("spawning", cardData["Name"])
		spawnUnit(cardData, team, slot)
		print("testing invokeclient")
		func_play_card:InvokeClient(plr, card)
func_play_card.OnServerInvoke = isValidPlay

From what I can tell from the output, pretty much everything in my previous code works (I tested with prints, my other functions seem to work as intended, and the problem points to the line when it runs:
func_play_card:InvokeClient(plr, card)
with the error:
Script that implemented this callback has been destroyed while calling async callback Client - uivisuals_n:70
pointing at line:
func_play_card:InvokeServer(card, i, newSlot)

I don’t know really much about remotefunctions or if I should use it, pcall(), which roblox dev wiki says to use with RemoteFunctions when invoking client. Should I even bother using remote functions to solve this problem?

Well, the error message is probably suggesting that at some point during the RemoteFunction bridge between client and server, the uivisuals_n LocalScript is being destroyed.

Perhaps your character is respawning and your ScreenGui's ResetOnSpawn property is enabled (try turning it off if not already).

That’s all I can think of for now.

1 Like

Thanks for trying, but ResetonSpawn is already off by default. :frowning:

Can you test your code again and see when exactly the error message prints out? Is there any code you have that actually manipulates or removes the LocalScript, even the slightest?

Checking my own player while playtesting, the localscript does not get destroyed when I run the test. On second thought, I might actually send a video. It prints right after the print(“testing invokeclient”) thing, as usual.

The only thing that has the keyword “script” in my local script is the line
for _, card in pairs(script.Parent.HandUI:GetChildren()) do

I believe you might be indirectly destroying your LocalScript because it’s inside the screenGui. Maybe you also have some card elements in there and you perform a destroy operation on all the children instead of just the cards- or whatever is relevant to your situation.

You could try moving your LocalScript to StarterPlayerScripts (and changing the paths) to see if your code no longer tries to destroy it by accident.

You might not have written the code that’s doing it, but something in there is. Any third party modules, etc.

Ok, the weirdest thing happened that fixed my code.

Looking at this article RemoteFunction:InvokeClient for reference I noticed that the script used only does client-to-server and does not use 2 invokes for client-server-client communication. I added a 2nd remote function and used that to invoke instead, and the code just worked lol.

I’m not sure if this is a terrible idea, as to me it seems like a remote function is no different from using 2 events, and plus, I’ve been told that client-server-client communication can break easily. You seem to know how remote functions work at least, is this abnormal or not? What should I look out for anyways?

remote functions are very different from remote events. remote functions can return information back to where it was invoked

I think I have noticed this, I have been using events for a while but pretty much just started using remote funcs this week.

I’m sure I’m writing spaghetti code that will come back to bite me in a few weeks, but alas, it’s a first game, and I guess I’ll call it a solution? I’m not sure.

You don’t really need to use RemoteFunctions at all. And I wouldn’t recommend using InvokeClient since an exploiter can force your server side code to yield forever by not returning a response (there’s ways you can circumvent the issue but why go around it? RemoteEvents can be used instead!)

You’d make a two-way remote event by using both OnServerEvent and OnClientEvent and both FireServer and FireClient which can basically replace your remote functions to an extent. You’d just send information as arguments as you normally do (rather than waiting for a return value on the requesting side).

@RatiusRat is still correct though.

Ok, but doesn’t that make remote functions irrelevant? If all I’d really be using are events.

I guess what I’m asking is when I would use a remote function instead.

edit: well actually nvm i guess i can’t read what i just learned lol

you can use 1 remotefunction for client to server to client communication which is more efficient and cleaner than 2 remote events.
so it’s useful for requesting data from the server.

For client to server communication, feel free to use a RemoteFunction or RemoteEvent depending on whether you require a response or not.

For server to client communication, just don’t rely on InvokeClient. An exploiter can hang your server side code (if it isn’t already running in a separate “thread”) by not returning a response, which you could also avoid by just using RemoteEvents.

If you want to be consistent, just use RemoteEvents for everything. If you don’t care about a little inconsistency, just mix between the remote types. Not sure this really matters.

1 Like

Re-reading the remote funcs and events page, I spotted the part that says “For client-only actions that don’t require a callback, like updating a GUI, a server-to-client remote event should be used instead.”.

I think I understand it a bit more now. Sorry for the trouble, I’ve got a headache too if that counts for anything.Ty.

1 Like