Instance is nil when firing from Server to Client

Hey there everyone,

today I made a script that fires to all clients when a pet is equipped, but on the client side it prints the pet as nil here is my module script:

-- SERVER
local PetsModule = {}
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local Functionality = game.ReplicatedStorage.Functionality
local Events = Functionality.Events
local AddPetMovementEvent = Events.AddPetMovementEvent

function PetsModule.EquipPet(PetMesh: MeshPart, Player: Player)
	
	local RootPart = Player.Character.PrimaryPart
	local s, PetsEquipped: Folder = pcall(function()
		return workspace[Player.Name .. "PetsEquipped"]
	end)
	if not s or not PetsEquipped then
		warn(PetsEquipped)
		return
	end
	local PetMeshClone = PetMesh:Clone()
	PetMeshClone.Anchored = true
	PetMeshClone.CanCollide = false
	PetMeshClone.CanTouch = false
	PetMeshClone.CanQuery = false
	PetMeshClone.Parent = PetsEquipped

	AddPetMovementEvent:FireAllClients(PetMeshClone)

end

return PetsModule

The module seems fine, I printed out the PetMeshClone Variable and it printed out correctly, but on the Client side it prints out nil, here is my script:

local pets = {}
local Events = game.ReplicatedStorage:WaitForChild("Events")
local AddPetMovementEvent = Events:WaitForChild("AddPetMovementEvent")

AddPetMovementEvent.OnClientEvent:Connect(function(pet)
    print(pet) -- prints nil for some reason ;-;
	table.insert(pets, pet)
end)

Help would be greatly appreicated!

Is it necessary to have the entire instance? RemoteEvents do not pass instances. Your best workaround would be to pass the pet’s name or any other information that you are going to use in the client.

1 Like

RemoteEvents can pass instances. The instance itself isn’t sent over the network, but its UID is. Instances on the server that are replicated to the client share the same UID, and the client reconciles which instance was sent over the network by finding the instance with that UID. This is all done under the hood, and the instance is successfully given as an argument to OP’s RemoteEvent.OnClientEvent callback. The problem here is that not enough time was allotted to allow the instance to replicate/finish replicating to the client, so the client is unable to connect the UID to an instance. You can verify this in Roblox Studio by delaying the deliverance of an instance after it’s created and parented to a mutually accessible area of the data model

2 Likes

RemoteEvents do pass Instances but they have to be visible by both the client and server at the time of passing

If a RemoteEvent or RemoteFunction passes a value that’s only visible to the sender, Roblox doesn’t replicate it across the client-server boundary and passes nil instead of the value. For example, if a Script passes a descendant of ServerStorage, the client listening to the event will receive a nil value because that object isn’t replicable for the client.

1 Like

Because of this, consider tagging the pet and using CollectionService:GetInstanceAddedSignal to detect its final appearance on the client, or use Instance.ChildAdded on the player’s equipped pets folder.

2 Likes

Print the PetsEquipped and tell me what it returns.

1 Like

it prints out a list of all the pets which is in a folder

PetMeshClone.Parent = PetsEquipped

So your parenting the clone to a table or a folder?

No. It is clear PetsEquipped is an instance. You can see this in the following code:

local s, PetsEquipped: Folder = pcall(function()
    -- A child of Workspace is being returned.
    return workspace[Player.Name .. "PetsEquipped"] 
end)

if not s or not PetsEquipped then
    warn(PetsEquipped)
    return
end

This is an unorthodox version of:

local petsEquipped = workspace:FindFirstChild(player.Name .. "PetsEquipped")
if not petsEquipped then
    warn("Could not find " .. player.Name .. "'s pets folder!")

    return
end

I don’t see anything wrong, possibly it can’t find the folder according to the key, double check the folder name and the key you provide.

It’s clear the pets folder is being found as the code is managing to fire to all clients. I stated the source of OP’s problem. It is the second reply to this post

its a folder containing the players pets, I think I could pass through the folder instead of the pet itself

Woah, aggressive response…

(Before you edited it lol)

Apologies. I did not mean for it to come off that way, hence my edit

1 Like

I think your best bet is to go with one of the two surefire solutions I offered. I would personally go with the second one