RemoteFunction not returning value

I am trying to return a cloned instance with a remotefunction, and it returns nothing on the client.

Server (prints the cloned instance fine, and returns it):

function spawnMag(mag: Instance)
	if mag then
		local m = mag:Clone()
		m.Parent = workspace
		return m
	end
end

remote.OnServerInvoke = function(player,name)
	local mag = mags:FindFirstChild(name)
	return spawnMag(mag)
end

Client Module (function fires and all, but the mag variable is nil?):

function module:SpawnMag(gunName: string)
	local name = gunName.."_Mag"
	local mag = event:InvokeServer(name)
	return mag
end

Not used to RemoteFunctions, and I belive I may have made an oversight.

Does the instance exist under mags ?

yes, the server successfully is able to clone it after I print the final server variable for it, it’s just that the client somehow doesn’t receive it

You gotta add a wait after parenting to workspace it’s just a replication issue.

1 Like

I was thinking a step.

local rns = game:GetService("RunService")
function module:SpawnMag(gunName: string)
    local name = gunName.."_Mag"
    local mag = event:InvokeServer(name)
    rns.Stepped:Wait()
    return mag
end

Stepped may still be a little to quick for the replication to take place. It takes time there’s definitely a better solution to this but Im on mobile.

I added a task.wait(player:GetNetworkPing) and it worked. You’d think they’d already wait for the replication, but I guess not.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.