Fast Cast and Part Cache Error

Hello, I keep getting this error,

“TestService: Exception thrown in your RayHit event handler: ReplicatedStorage.Dependencies.PartCache:132: Cannot statically invoke method ‘ReturnPart’ - It is an instance method. Call it on an instance of this class created via PartCache.new”

I’m not sure why this is happening I used an old fastcast script I had and tried changing it for Part Cache like shown in this tutorial

function fireAtCharacther()
	local closestPlayer = AI.Get(script.Parent.HumanoidRootPart.Position)
	if closestPlayer then
		local distance = (dummy.HumanoidRootPart.Position - closestPlayer.Position).Magnitude
		if distance <= 15 then
			local dummyRoot = dummy.HumanoidRootPart
			if distance <= 15 then
				if canFire then
					print("FIRED")
					canFire = false
					params.FilterType = Enum.RaycastFilterType.Exclude
					params.FilterDescendantsInstances = { dummy}
					behavior.RaycastParams = params
					behavior.Acceleration = Vector3.new(0, gravity, 0)
					behavior.AutoIgnoreContainer = true
					behavior.CosmeticBulletProvider = ball
					behavior.CosmeticBulletContainer = workspace.EnemyDodgeballs

					
					local direction = (closestPlayer.Position - dummy["Right Arm"].Weapon.Union.FireAttachment.WorldPosition).Unit


					caster:Fire(dummy["Right Arm"].Weapon.Union.FireAttachment.WorldPosition ,direction, speed, behavior)		
					task.wait(.5)
					canFire = true
				end
				task.wait()
		end
	end
end
end


caster.LengthChanged:Connect(function(ActiveCast, lastPoint, rayDir, displacement, segmentVelocity,  cosmeticBulletObject)

	local newPos = lastPoint + (rayDir * displacement)
	cosmeticBulletObject.Position = newPos

end)

caster.RayHit:Connect(function(ActiveCast, RaycastResult,  segmentVelocity, cosmeticBulletProvider)

	local hitPos = RaycastResult.Position
	local hit = RaycastResult.Instance
	local characther = hit:FindFirstAncestorOfClass("Model") or hit.Parent
	local humanoid = characther:FindFirstChildOfClass("Humanoid")

	print(hit)

	if humanoid and game.Players:GetPlayerFromCharacter(humanoid.Parent) then
		humanoid:TakeDamage(20)
	end
	cache:ReturnPart(cosmeticBulletProvider)
end)




while true do
	task.spawn(getClosestPlayer)
	task.spawn(turnTowardsCharacther)
	task.spawn(fireAtCharacther)
	task.wait()
end

You dont set settings of the fastcast in a function, setung it up in global. Only calculate direction then fire it in the function.

The fastcast.CosmeticBulletProvider should set the the partcache rlike the tutorial, not the ball

1 Like