Part is ''null'' on the client

Hello, I’m trying to make a server-spawned part tween on each client, however, I’m met with this error:

Part of the server script:

	local newCandy: Model = candy:Clone()
	newCandy:PivotTo(candySpawnPart.CFrame * CFrame.new(math.random(-rangeX,rangeX), 12.5, math.random(-rangeZ,rangeZ)))
	
	newCandy.Parent = candyFolder
	
	local hitbox: Part = newCandy:FindFirstChild("Hitbox") :: Part
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {candyFolder}
	raycastParams.FilterType = Enum.RaycastFilterType.Include
	
	local raycastResult = workspace:Raycast(hitbox.Position, hitbox.Position - Vector3.new(0, 30, 0), raycastParams)
	if not raycastResult then newCandy:Destroy() candyModule.spawnCandy() return end
	
	candyAnimation:FireAllClients(hitbox, raycastResult.Position)

Client script:

local ReplicatedStorage: ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService: TweenService = game:GetService("TweenService")

local candyAnimation: RemoteEvent = ReplicatedStorage:WaitForChild("CandyAnimation") :: RemoteEvent

local tweenInfo: TweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Bounce,
	Enum.EasingDirection.Out
)

local function onClientEvent(hitbox: Part, targetPosition: Vector3)
	local tween: Tween = TweenService:Create(hitbox, tweenInfo, {CFrame = CFrame.new(targetPosition)})
	tween:Play()
end

candyAnimation.OnClientEvent:Connect(onClientEvent)

Any help is appreciated.

2 Likes

If your part is placed in a location that client cannot access (ex. ServerStorage, ServerScriptService), then the function will return nil.

You can’t pass instances through remotes. You should instead pass something it can be identified by. It seems that you can pass instances through remotes when using the Studio command bar (i.e. not in the proper Roblox environment) but not through scripts at runtime.

2 Likes

You hace the streaming enable of workspace? Disable,

The ‘‘hitbox’’ is parented to ‘‘newCandy’’. newCandy is put in ‘‘candyFolder’’, which is parented to the workspace.

This si correct, better send the name

Then instead, try to fetch the part from client in the CandyFolder. (Example: Pass the part name and look for the part with its name inside the folder)

1 Like

Make sure the candy is in a place the client can access, and that the candy is within render distance of the character if you have streaming disabled

Possibly the remote event is received by the client before the instance is loaded by them, so they think it doesn’t exist. I’ve never had any trouble passing instances in remote events

You could:
[A] Give the client a :WaitForChild() event on the folder that the candy will be parented to and communicate the values through attributes on the candy instead of communicating these things through a remote event to the client, which would work well if the client only needs to load things within their render distance
[b] Yea as the others said, give the candy a unique name and then tell the client the name of the candy to look for, but if you have StreamingEnabled make sure you don’t give the client a potentially infinite :WaitForChild on something it’ll never see