Onclientevent parameter being recieved as "nil"

I’m trying to make a system in which a ball spawns in, and chooses a random target. At the moment I’m working on the targeting part of the script. I’m currently trying to send the Ball to the client so that the client can edit the ball’s properties. The main issue is that the parameters are being received as “nil.” The ball is located in the Workspace, and I’ve checked, but I haven’t been able to find a solution to this problem.

Client code:

local CAS = game:GetService(“ContextActionService”)
local Sword_Event = game:GetService(“ReplicatedStorage”):WaitForChild(“Sword_Events”):WaitForChild(“Sword_Event”)
local On_Target = game:GetService(“ReplicatedStorage”):WaitForChild(“On_Target”)

On_Target.OnClientEvent:Connect(function(Ball)
print(“Ball”) – printed as “nil”
end)

Server code:

local On_Target = game:GetService(“ReplicatedStorage”):WaitForChild(“On_Target”)
local Ball = script.Parent
local Target = Ball:WaitForChild(“Target”)

Target.Changed:Connect(function()
local Character = Target.Value
print(Character.ClassName)

local Player = game.Players:GetPlayerFromCharacter(Character)
print(Player)

print(Ball.Parent)
On_Target:FireClient(Player, Ball)

end)

3 Likes

Non-Replicated Instances

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.

2 Likes

but the ball is in the workspace?

1 Like

isn’t this supposed to be print(Ball)??? adding “” just prints text for me

1 Like

oh ye it’s supposed to be print(ball) but that’s where it’s “nil”

Just try to print out a string, like print(Ball.Name)
Since yeah, Ball is an object.

It may be that the ball is not being replicated in time when the event is received on the client.

Try this pattern:

Ball.Parent = workspace

task.defer(function()
   On_Target:FireClient(Player, Ball)
end)

Edit: it may need to be task.delay(0, function() … end) instead actually so worth trying both