Weird problem with raycast hitbox i'm making

I didn’t want to post this but i just can’t figure out the problem. So i’m making a raycast hitbox but there is this weird issue. (I know the module but i’m doing this for learning purposes)

The issue is, there is a “lag” or something i can’t really explain but its definetly not right.

https://gyazo.com/57e698774a1648e26688c3a892efd4d8
This is the GIF of it, as you can see its laggy and sometimes stops too early or goes reverse, sometimes it does work the intended way but its rare.

I have looked through the forum and reviewed my code many times, made alot of changes but nothing worked.

local Debris = game:GetService("Debris")
local Remote = game.ReplicatedStorage.Hitbox


function StartHitbox(Object,MaxTime,DebugMode)
local TimerOut = false
task.delay(MaxTime,function()
TimerOut = true
end)
local Attachments = {}
local ToBlacklist = {Object}
local Goal = nil


for _,v in pairs(Object:GetDescendants()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
 table.insert(ToBlacklist,v)
end
end

for _,v in pairs(Object:GetDescendants()) do
 if v:IsA("Attachment") and v.Name == "HitboxAttachment" then
  table.insert(Attachments,v)
 end
end

repeat
task.wait()
for i = 1 , #Attachments do

 local Attachment = Attachments[i]

 local PreviousP = Attachment.WorldPosition

 task.delay(0.005,function()
 local CurrentP = Attachment.WorldPosition
 

 if PreviousP ~= CurrentP and Goal == nil then

 local RaycastP = RaycastParams.new()
 RaycastP.FilterType = Enum.RaycastFilterType.Blacklist
 RaycastP.FilterDescendantsInstances = ToBlacklist
 

 local RaycastResult = workspace:Raycast(PreviousP, (CurrentP-PreviousP),RaycastP)

    if RaycastResult then 
     if RaycastResult.Instance.Parent:FindFirstChild("Humanoid") then
     Goal = RaycastResult.Instance
     end
    end
    
    if DebugMode == true then
    local Distance = (CurrentP-PreviousP).Magnitude
    local BeamCFrame = CFrame.new(CurrentP, PreviousP)
  
	local DebugPart = Instance.new("Part")
	DebugPart.BrickColor = BrickColor.new("Sea green")
	DebugPart.Material = Enum.Material.Neon
	DebugPart.Anchored = true
	DebugPart.CanCollide = false
    DebugPart.CanQuery = false
	DebugPart.Name = "DebugPart"
	
	DebugPart.Size = Vector3.new(0.1, 0.1, Distance)
	DebugPart.CFrame = BeamCFrame * CFrame.new(0, 0, -Distance/ 2)
	
	DebugPart.Parent = workspace.DebugParts
    Debris:AddItem(DebugPart,0.2)
   end

  end
 end)

end
until Goal ~= nil or TimerOut == true
if Goal ~= nil then
return Goal
end
end


Remote.OnServerEvent:Connect(function(Player,Time,DebugMode)
local Result = StartHitbox(Player.Character,Time,DebugMode)
if Result then
 print("Found "..Result.Name)
end
end)

This is the script, normally the green lines (debug parts) are made locally but i changed it to server temporarily to debug the issue. Also it works perfectly when the attachments are inside a part and i play in run mode. Thanks.

1 Like

Are you using any modules (e.g. RaycastHitboxV4 or ClientCast)?

I would definitely recommend ClientCast as it practically removes/reduces hit delay by calculating the raycasts on the client (instead of the server). This also imposes a slight risk of exploitability, but that can be easily fixed by implementing your own hit validation system. The benefits pretty much outweigh the risks.

1 Like

I’ll do it in client then, thanks.

1 Like

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