Remote Function returning nil

This code is resulting in the return from GrabServerSided.OnServerInvoke being nil for a reason I dont know.

server:

game.ReplicatedStorage.GrabServerSided.OnServerInvoke = function(plr,mouseTarget,grabPosition)
    local armReplica = plr.Character["Right Arm"]:Clone()
    armReplica.Parent = plr.Character
    armReplica.Anchored = true

    local armMesh = Instance.new("SpecialMesh",armReplica)
    armMesh.MeshId = "rbxasset://fonts//rightarm.mesh"

    local rope = Instance.new("RopeConstraint",armReplica)
    local att0 = Instance.new("Attachment",armReplica)
    local att1 = Instance.new("Attachment",mouseTarget)    
    
    att0.WorldPosition = armReplica.CFrame:ToWorldSpace(CFrame.new(Vector3.new(0,-armReplica.Size.Y/2,0))).Position
    att1.WorldPosition = grabPosition

    rope.Attachment0 = att0
    rope.Attachment1 = att1

    return armReplica
end

local:

    local armModel = game.ReplicatedStorage.GrabServerSided:InvokeServer(mouse.Target,grabPosition)    

    print(armModel)
2 Likes

Print the arm model on the parent before returning it. Do a double check to make sure it still exists.

1 Like

I can confirm the parent is being printed on the server but not on the client

1 Like

is the “Archivable” property of the part set to true? if not then cloned parts will appear nil

2 Likes

The archivable property is true

1 Like

Oh this is probably due to the function returning before it’s actually parented.
A thing that happens a lot when using invokes especially when returning instances is that it’ll just return the instance as soon as it’s created, and won’t wait for it to be parented.

try yielding until armReplica.Parent ~= nil

alternatively, just have the client do :WaitForChild(), and turn it into a RemoteEvent instead.

1 Like

That’s because the server is returning the instance as soon as it gets created, it doesn’t yield for the rest of the script.

1 Like

So I recommend doing this instead: (WaitForChild) is probably more efficient, since you won’t be holding up the server.

1 Like

i think this bug will only happen if you have StreamingEnabled on in the workspace properties, and it is happening because the cloned part takes time to replicate to the client. See if turning off streamingenabled fixes the issue

1 Like

No, it doesn’t occur due to streaming enabled, it’s due to how RemoteFunctions work, RemoteFunctions are directly connected to server runtime, Remotes do their best to return values as quick as possible to prevent infinite yields and server memory leaks. Hence why when you use remotes to return a value, as soon as the value itself does not equal nil, it will be returned.

Since think of return with RemoteFunctions as promises, they are asynchronous with the function to maintain the best performance for the server.

i recreated the situation in a test place and it returns nil when streamingenabled is on, and returns fine when it is off

2 Likes

Edit

I take back my statement, it could be caused due to streaming enabled, sorry, since streaming enabled does affect the replication runtime efficiency to workspace. (Synchronizes workspace changes, Improved performance section)
Dev forum:

2 Likes

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