How to optimize my blood raycasting system?

local function checkCollision(part, grabbed)
	local directions = {
		Vector3.new(1, 0, 0),   -- Right
		Vector3.new(-1, 0, 0),  -- Left
		Vector3.new(0, 1, 0),   -- Up
		Vector3.new(0, -1, 0),  -- Down
		Vector3.new(0, 0, 1),   -- Forward
		Vector3.new(0, 0, -1)   -- Backward
	}

	local rayLength = part.Size.Magnitude
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Terrain.Blood, workspace.Characters, workspace.Cubes}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	for _, dir in pairs(directions) do
		local rayOrigin = part.Position + Vector3.new(0,1,0)

		if grabbed then
			rayOrigin = part.Position
		end

		local rayDirection = dir
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

		if raycastResult and raycastResult.Instance then
			local hit = raycastResult.Instance
			if hit:IsA("BasePart") and hit.CanCollide then
				return true, hit, raycastResult.Normal, raycastResult
			end
		end
	end

	return false
end

part.Touched:Connect(function(child)
		local hasCollision, hit, normal, result = checkCollision(part, grabbed)

		if hasCollision then
                      -- blood placement system
                end
end)

checks lead to fps loss, how i can fix it?

fps loss? well my projectile systems with 100+ projectiles at once give me the same fps, so I am kind of confused.

Maybe the part is being touched way too much? You are checking raycasts like 7 times.
Does it lag without the blood part of the script? Just running the collision function.

Your part’s touched event is connected to a function that, as far as I can see, doesn’t perform any debouncing. This allows part to trigger checkCollision every frame. Given that checkCollision is executing some more performance-heavy methods, I suspect this will lead to performance drops. If you don’t need the touched event to fire every frame, try adding a debounce function to it. Better yet, if you only need to check for initial contact, add it to the raycast params’ blacklist and remove it after you’re done with a touch ended event.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.