Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i make script send back a variable to local through Remote Function?

I'm not any good with Remote Functions , neither Return, I'm making a building tool, and it sends a variable to the server containing the AssetId of the object to add and if it uses useAssetVersionId or not.

This is the Line in the Local Script

	local root -- this is nothing rn
	if useAssetVersionId then
		Insert:InvokeServer(true, assetId)
	else
		Insert:InvokeServer(false, assetId)
	end

Here is the server script:

local remotes = game:GetService("ReplicatedStorage")
local Insert = remotes:WaitForChild("Insert")
function onInsert(player,useAssetVersionId,assetId)
	if useAssetVersionId == true then
		local root = game:GetService("InsertService"):LoadAssetVersion(assetId)
		return root
	else
		local root = game:GetService("InsertService"):LoadAsset(assetId)
		return root
	end
end
Insert.OnServerInvoke = onInsert

so basically I want the Remote Function to send back to client the defined version of the variable "root", but line 6 and 9 in the server script pretty much does nothing, any help would be very helpful

sorry for this question tho lol

1 answer

✓ Accepted answer

Hello sir, before we continue further I'd recommend something.

I discourage what you are doing, sending a signal to the server in order for it to load the asset seems pointless as you're doing nothing but returning a loaded AssetId, however if you already knew about this then you must be trying to create something on the client side, while it still having a value on the server, in that case ignore this.

Now to answer your question.

When something is returned, it will be set as a variable when calling the function.

function add(num, amount)
	return num + amount
end

local number = 1

number = add(number, 2)

print(number)

-- Output: 3

In the function above we simply add a number to the already defined number variable and redefine it, which is similar to what you're trying to do.

Like functions, remotefunctions are the same, they return their values through variables so your LocalScript should look something like this.

local root -- this is nothing rn
if useAssetVersionId then
   root = Insert:InvokeServer(true, assetId)
else
   root = Insert:InvokeServer(false, assetId)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.