RemoteFunction returns nil

I am trying to send a model from the server to a client using RemoteFunction and InsertService. However, when returning the model to the client, the model turns NIL.

Anyone has any idea what I’m doing wrong, or if this is a common thing?

Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local modelId = randomModelID

local remoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

local model = remoteFunction:InvokeServer(modelId)

print(typeof(model)) -- Returns nil

model.Parent = ReplicatedStorage -- Returns error: attempt to index nil with 'Parent'

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local InsertService = game:GetService("InsertService")

local remoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")

local function returnModel(_, modelId)
    local model = InsertService:LoadAsset(modelId)
    print(typeof(model)) -- Returns as "Instance"
	return model
end

remoteFunction.OnServerInvoke = returnModel

Thanks!

1 Like

The object only exists on the server when you’re returning it:

You would need to re-parent it to somewhere the client can see it (like ReplicatedStorage)

1 Like

I thought this way would work the same as placing it in ReplicatedStorage. Anyways, I don’t want it to go through any other players, I just want the specific client to receive the model.

1 Like

Unfortunately, Roblox doesn’t give tools for only a single client to see the object, so you would have to let every client see it. Though, what you do with the model in particular can be client specific.

That may actually work; I didn’t think about that. Wish there was a better way though.

Oh. That’s quite unhandy. Since I’m working with big models, having them go through every single client makes it quite unefficient. It’s weird that you can basically send anything to the client using remoteEvents or remoteFunctions, but somehow objects need to be replicated to every single client.

Is this what you are looking for:

I wrote this real quick, but it clones a model from replicated storage to the Client’s workspace only.


player = game:GetService("Players").LocalPlayer
model = game:GetService("ReplicatedStorage").Model
button = script.Parent
button.Activated:Connect(function()
local modelClone = model:Clone()
modelClone.Parent = game:GetService("Workspace")
end)

Parent the model to the player’s PlayerGui. This is the only way to replicate an instance to a single client.

local function returnModel(Player, modelId)
    local model = InsertService:LoadAsset(modelId)
    model.Parent = Player.PlayerGui
	return model
end

I’ve made a post once before regarding how we should have a more intuitive way to do this.

2 Likes

Yep, that did it for me. Weird how roblox doesn’t have a single different solution for this. Thanks though!

You’re welcome, glad it worked for your use case.

Yeah, that’s because the PlayerGui has a special tag.

1 Like

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