Melee Raycasting Performance

  1. What do you want to achieve?
    A performant melee system using attachments along the blade to raycast every interval of time.
    image

  2. What is the issue?
    Absolutely horrible performance. 10% script usage per swing!!! Any advice and criticism is welcome.

elseif args.input == attackInput then

		local weaponName = valuesModule.findValue(player, "weapon")
		local weapon = rightArm:FindFirstChild(weaponName)

		if weapon then
			
			local swingDuration = weaponData[weaponName]["swingDuration"]
			
			local attatchments = {}
			
			for _, v in pairs(weapon:FindFirstChild("Attatchments"):GetChildren()) do
				
				table.insert(attatchments, v)
			end
			
			for _, v in pairs(attatchments) do
				
				task.spawn(function()
					
					local timer = 0
					
					while true do

						if timer >= swingDuration then
							break
						end
						
						print(v)
						
						timer += .1
					
						local positions = {}

						table.insert(positions, v.WorldPosition)
						task.wait(.1)
						table.insert(positions, v.WorldPosition)

						local rayDirection = (positions[1] - positions[2]).Unit
						local rayParams = RaycastParams.new()

						local rayResult = workspace:Raycast(positions[1], rayDirection, rayParams)				

						if rayResult then
							print(rayResult.Instance)
						end

					end
				end)
			end
		end

P.S. I am aware of the RaycastHitbox open-source module. I would like to make my own system is all.

Because I’m not the type to give those huge in-depth paragraphs explaining what you should do, I highly recommend checking out Roblox multi-threading/parallel execution or this Youtube video.

I’m not sure how much more performant this will be, but place your script under an actor and test this edited code.

elseif args.input == attackInput then
	local weaponName = valuesModule.findValue(player, "weapon")
	local weapon = rightArm:FindFirstChild(weaponName)
	if not weapon then return end

	local swingDuration = weaponData[weaponName]["swingDuration"]
	local attatchments = weapon:FindFirstChild("Attatchments"):GetChildren() 

	local function Process(Attachment)
		local StartTime = os.clock()

		task.desynchronize()
		while (os.clock() - StartTime) <= swingDuration do
			local InitialPosition = Attachment.WorldPosition
			task.wait(.1)
			local Direction = (Attachment.WorldPosition - InitialPosition).Unit

			local Raycast = workspace:Raycast(InitialPosition, Direction)				

			print(Raycast and Raycast.Instance)
		end
		task.synchronize()
	end

	for _, v in attatchments do
		task.spawn(Process, v)
	end
end

Edit: I don’t really use parallel scripts that much, so I might’ve used it incorrectly

That is dramatically better. It doesn’t even hit .1% with five attatchments! Very nice work, and thank you very much.

I do get this warning though:
task.synchronize() should only be called from a script that is a descendant of an Actor

Change your Workspace’s Signal Behavior to Immediate

I’m totally fine with doing that, but isn’t roblox moving to Deferred?

Yea, they did but I remember a post where an actual internal roblox dev talked about how it has funky behavior when its under deferred signal behavior.

Ah, I guess when they fix that I’ll go to deferred then. Thanks!

Oh, by the way, what was your reasoning for not using coroutines?

The task library uses coroutines, and they’re in serial, which means they run back to back with other threads that are running

Ah ok. character limitttttttttt

Try to make the raycast params so that only the important hitboxes of the map and player hitboxes get affected. Raycasting the whole workspace is not performant

Ah so make an Include raycastparams and add every character part?

Yup, this should increase the performance by a lot hopefully. You can spam raycasts comfortably (betweenf frames) if you have a raycast params with low amount of objects

It would be better if you excluded the entire map if it’s under one folder called something like “map”. This would be better than looping and including everything you want to detect + why would u need to detect the map for a sword that detects player hits?

Why is it a 10% difference in script usage by just filtering out the baseplate and spawn location?

It starts to matter when your game gets massive and you end up having alot of instances in the workspace. This would significantly slow down checking raycasts but filtering allows you to ignore some instances saving you time

Wait… could the length of the raycast be contributing…

Alright guys lil update:

  • added length to the raycast (minimal performance gain)
  • i filtered all descendants of the weapon itself (massive performance gain about 10%)

(1) edit after some testing: script usage reaches 1% @ 400 parts in the workspace, but thats with the swing lasting 5 seconds which isn’t obviously going to be what happens

(2) edit after more testing: script usage reaches 1% @ 1000 non-map parts in the workspace, still with 5 second swing for testing purposes

  • added map folder (baseplate, spawn, and later the actual map) which makes it more consistent, but doesn’t necessarily give a huge performance gain
  • now filter excludes character as well (like a .1 percent gain)

I forgot something… For your script you need to multiply your ray cast direction by the distance between the two positions. And no i don’t think it contributes too much because raycasts detect the first object that’s hit then it stops