OnClientEvent Part parameter being received as nil

I am trying to make it so when a ball is spawned it is half transparent to everyone else but 0% transparent the the person who spawned the ball, I keep getting an error the that ball parameter is getting received as nil. Does anyone know what is wrong?

Server Script:

-- Get necessary services
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")

-- Define tween properties
local tweenGoal = {Size = UDim2.new(1, 0, 0.525, 0)}
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true)

-- Retrieve all box objects and pop sounds
local boxes = Workspace.Boxes:GetChildren()
local tweens = {}

-- Create tweens for each box
for _, box in ipairs(boxes) do
	tweens[box.Name] = TweenService:Create(box.SurfaceGui.Frame.TextLabel, tweenInfo, tweenGoal)
end

-- Function to handle ball drop
ReplicatedStorage.ClientToServerEvents.DropBall.OnServerEvent:Connect(function(player)
	local touchDebounce = false
	local randomOffset = math.random(-50000000, 50000000) / 1000000000

	-- Ensure randomOffset is not zero
	while randomOffset == 0 do
		randomOffset = math.random(-50000000, 50000000) / 1000000000
	end

	-- Clone the ball and set its position
	local ball = ServerStorage.Ball:Clone()
	ball.CFrame = ball.CFrame + Vector3.new(randomOffset, 0, 0)
	ball.Parent = Workspace.Balls
	ball.Transparency = 0.5
	game.ReplicatedStorage.ServerToClientEvents.DropBallSpawnClient:FireClient(player, ball)

	-- Ball touches box event
	ball.Touched:Connect(function(hit)
		if touchDebounce == false and tweens[hit.Name] then
			touchDebounce = true
			game.ReplicatedStorage.ServerToClientEvents.DropBallDestroyClient:FireClient(player, hit, tweens)
			ball:Destroy()
		end
	end)
end)

Client Script:

-- Client script to handle server events

game.ReplicatedStorage.ServerToClientEvents.DropBallSpawnClient.OnClientEvent:Connect(function(ball)
	ball.Transparency = 0
end)

game.ReplicatedStorage.ServerToClientEvents.DropBallDestroyClient.OnClientEvent:Connect(function(hit, tweens)
	if tweens[hit.Name] then
		tweens[hit.Name]:Play()
	end

	for _, v in pairs(hit:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			v:Emit(50)
		end
	end

	local sounds = script.Parent.PopSounds:GetChildren()
	local soundIndex = tonumber(hit.Name)
	if soundIndex and sounds[soundIndex] then
		sounds[soundIndex]:Play()
	end
end)
1 Like

You need to make sure the ball is actually parented to the Workspace before you send it to the client. So before you send the DropBallSpawnClient event, add this line of code:

repeat task.wait() until ball:IsDescendantOf(Workspace)

This isn’t necessary when you can literally just

local ball = workspace:WaitForChild("Ball")
print("Ball is now a descendant of Workspace")

With this I am still getting: DropBallClient:4: attempt to index nil with ‘Transparency’

There will be multiple Balls named “Ball” so I don’t think this would work

Try setting the transparency before parenting it to workspace? The ball may also be despawning when parented to workspace.

Alrighty

Jeez same error

1 Like

You can’t send an instance that you create on the server as an argument for a remote event to the client due to argument limitations

You could rename the ball to the player’s user id on the server, then pass the user id instead in the remote event. Then the player can just get the part by doing workspace.Balls[userid]

Or you could add an attribute to the ball on the server, set it to the player’s user id, then just ditch the remote event entirely and on the client listen to workspace.Balls.ChildAdded and check if the added instance has the correct attribute

3 Likes

What happens if there are multiple balls with the same userID?

Well if you only need to change the transparency then it shouldn’t change anything since the only thing the client has to do is check if each ball has their user id and the client from there can change the transparency of them no matter how many there are