Help with hitbox system!

Hello, I’m wondering how to fix my hitbox system. The current problem(s):

local Hitbox1 = script.Parent["Right Arm"]:WaitForChild("Handle").Holder.Blade.Hitbox1
local Hitbox2 = script.Parent["Left Arm"]:WaitForChild("Handle2").Holder.Blade.Hitbox2

local Debris = game:GetService("Debris")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DamageStart = ReplicatedStorage.DamageStart
local DamageEnd= ReplicatedStorage.DamageEnd

local BreakLoop = false

local counter = 0

function StartHitboxes()
	BreakLoop = false
	counter = 0

	while true do
		task.wait(0.01)
		
		if BreakLoop == true or counter == 20 then
			break
		end
		
		local Hitbox1Clone = Instance.new("Part", workspace)
		local Hitbox2Clone = Instance.new("Part", workspace)
		Hitbox1Clone.Name = "Hitbox1"
		Hitbox2Clone.Name = "Hitbox2"
		Hitbox1Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox2Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox1Clone.Anchored = true
		Hitbox2Clone.Anchored = true
		Hitbox1Clone.Transparency = 0.5
		Hitbox2Clone.Transparency = 0.5
		Hitbox1Clone.CanCollide = false
		Hitbox2Clone.CanCollide = false
		Hitbox1Clone.Color = Color3.new(1, 0, 0)
		Hitbox2Clone.Color = Color3.new(1, 0, 0)
		Hitbox1Clone.Massless = true
		Hitbox2Clone.Massless = true
		Hitbox1Clone.Position = Hitbox1.Position
		Hitbox2Clone.Position = Hitbox2.Position
		Hitbox1Clone.Orientation = Hitbox1.Orientation
		Hitbox2Clone.Orientation = Hitbox2.Orientation
		Hitbox1Clone.CanTouch = true
		Hitbox2Clone.CanTouch = true
		Hitbox1Clone.CanTouch = false
		Hitbox2Clone.CanTouch = false
		Debris:AddItem(Hitbox1Clone, 1)
		Debris:AddItem(Hitbox2Clone, 1)
		counter += 2
	end
end

function StopHitboxes()
	BreakLoop = true
end

DamageStart.OnClientEvent:Connect(function()
	StartHitboxes()
end)

DamageEnd.OnClientEvent:Connect(function()
	StopHitboxes()
end)
3 Likes

Why not just add in a hitbox then use a weldconstraint to attach it to the player?

2 Likes

Man i don’t know where to start.

Your Hitboxes Should be done on the Server! (It Will Make Damaging Players Easier Just By Using

[Workspace:GetPartsInPart()] For Detecting Players In Specific Hitbox

If You Want To Keep Using Hitboxes You Can But I Prefer To Use RayCast Calculations and its also better to use (Actors) For Performance On The Server

Over All Your Issue Is Still UnSolved

Also What Are You Doing Here!?
image

CanTouch to true and then Immedietly to false ? Imma i Missing Something ?

I Would Also Suggest Using RenderStepped if your gonna keep Using the Client Method!

2 Likes

Apologies for the messy script. I’m new to scripting and don’t know what render stepped is. please help

Because they need to be constantly cloned when swinging in order to have a more accurate hit.

that’s where the docs will become real helpful.
RenderStepped is an event that’s pretty much called every time a new frame is rendered (don’t know the exact part of the rendering process that it’s called but it’s something like that)

1 Like

How is this typed out in my localscript?

1 Like

At the start of the script you’ll need to get RunService. Based on your code you should know how to do this

Then just connect the RenderStepped event to a function.

For example

runservice.RenderStepped:Connect(function()
-- this will run every single frame
end)
1 Like

when you say “This will run every single frame” do you mean it’ll clone every single hitbox? or…

whatever code you put inside the event will run every single event. for example

runservice.RenderStepped:Connect(function()
  print("hello")
end)

would print hello every single frame.

1 Like

for this, how would I know how many frames there are?

I’m just not sure how to implement this into my code either :confused:

You could keep a counter variable to keep track of how much frames have passed

local frames = 0

rs.RenderStepped:Connect(function()
  frames += 1
  print(frames.." frames have passed!")
end)

Also, I’m not sure of what you wanted to achieve in the beginning, I just saw the most recent comment from you asking about how RenderStepped worked so I just came in to explain that. Sorry about that.

What do you mean more accurate? If you weld the hitbox to the sword then it would follow the sword wherever it goes with basically no delay

1 Like

I agree with him. Welding a part to the sword seems the best approach.

1 Like

Where is the code that does damage? Looks like your hitboxes are empty you must connect a function like:

Hitbox1Clone.Touched:Connect(doesDamage)

You must remove this:

		Hitbox1Clone.CanTouch = false
		Hitbox2Clone.CanTouch = false

I make it create mutliple hitboxes so it could hit multiple people at the same time during a swing. and also having more frequent hitboxes has more touch accuracy.

I think you may want to use this raycast hitbox for your melee attacks. It works by constantly (and effeciently) raycasting between where a hitbox is and where it was one step ago. This prevents the need to constantly spam hitboxes for them to be accurate to where the sword is.

BasePart.Touched:Connect() is unreliable and buggy. Only ever use it if you need to check when a part is touched, not when it is inside something.

3 Likes

I can vouch for what @TheCraftNCreator suggested. I have used it in the past, and it is a really good module.

You are absolutely right, thank you for telling me.