Instance returning as nil when transfered from server to client using remote event

I’m trying to use a remote event to fire from the Server to the Client. In this specific script, I am trying to get a humanoid I have just cloned on the server and then transfer that to the client so I can set it as the camera subject.

Whenever I transfer the humanoid that was cloned, it returns as nil after being sent to the client.

Server Script:

prompt.Triggered:Connect(function(player)
	local fighter = game.ServerStorage.Fighter

	local fighter1 = fighter:Clone()
	fighter1.Parent = workspace
	local fighter1human = fighter1.Humanoid
		
	player.Character = fighter1
	AlignCamera:FireClient(player, fighter1human)

end)

Player returns correctly as the player’s name. Fighter1human returns correctly as being Humanoid.

Local Script:

AlignCamera.OnClientEvent:Connect(function(player, fighter1human)
	camera.CameraSubject = fighter1human
end)

Player returns incorrectly as Humanoid. Fighter1human gets an error as it is returning as nil, and has no name.

Everything else about the script works fine, it’s just that one part that is returning incorrectly. Am I using OnClientEvent:Connect incorrectly? Is it impossible to transfer information from the server to the clint like this, or should I be using another way? Am I just making a simple mistake? Any information is helpful, thank you.

Try putting it in Replicated Storage so the client can access it

RemoteEvent.OnClientEvent doesn’t receive player as the first argument because player can already be accessed on the client. Your connection should look:

AlignCamera.OnClientEvent:Connect(function(fighter1human)
	camera.CameraSubject = fighter1human
end)
2 Likes

I’ll try this when I get home today, thanks

For anybody else stumbling upon this in the future, like I am - this is not a solution. Instances cannot be passed through remote events for reasons unknown.
I will edit this reply once I find a real solution.

Instances can be passed over remote events, but its possible the remote event will fire and be received before the instance replicates.

Can you elaborate? Instances can be passed through remotes. However, Instances that are only created on the server, or only created on the client cannot pass the boundary because they only exists in those respective contexts.

(i.e. Doing Instance.new on the client prevents that Instance from being passed to the server, or parenting an Instance to ServerStorage/ServerScriptService does not allow it to be passed to the client)

Then I’ve been misinformed by yet another forum post I read previously. My own testing mirrored that to be true so I assumed it was. However, after adding a massive, 3 second wait() to my script, I found that instances can infact be sent over remote event. I apologize for being so confidently wrong.

However, there’s still an issue with your reply - it’s possible that the remote event will fire before the cloned fighter is replicated to the client. Meaning the instance would appear as nil to the client when sent.
I realize now that I’m facing a similar issue - where my remote event might fire before the player fully loads into the game. Since there is no way to check when the player is fully loaded (as far as I can tell), I’m stumped on how to fix it.

What I usually do this case is have the client fire a RemoteEvent to the server once it’s detected that it has loaded:

-- ReplicatedFirst LocalScript
if not game:IsLoaded() then
    game.Loaded:Wait()
end

RemoteEvent:FireServer() -- Tell the server that the client has loaded

But I’m able to do this because the structure of my game allows me to; I don’t know if your game could work with this setup. I hope you find a solution for your problem!

Unfortunately my game does not work with this setup due to an issue with game:IsLoaded/game.Loaded.
They do not check if the children of models are loaded in. Only if the models themselves are loaded in. I learned this when doing something unrelated with loading.
But with this loading issue I keep running into on my scripts… maybe I’ll just stop using models.
Thank you for your help!