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)
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.
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
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.
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
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