What do you want to achieve?
A performant melee system using attachments along the blade to raycast every interval of time.
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
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
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.
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
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?
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
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