How do I make a punch hitbox?

So I’m trying to make a hitbox appear in front of the player when they punch, and that hitbox can’t damage the player themselves, rather the other players.

I tried naming the part after the player, and in the hitbox damage script I put if hit.Parent.Name ~= (""..script.Parent.Name.."") yet it still hits the player, even though I even tried printing the name out and it read as the player.

I’m also having trouble locking the hitbox part infront of the player, it more just slowly hovers and moves infront of them.


script.Parent.OnServerEvent:Connect(function()

local newItem = game.ReplicatedStorage.HitBoxes.M1s.PunchHitbox:Clone()
	
newItem.Name = (""..Character.Name.."")
newItem.Parent = workspace
	
	for i = 1,10 do
	
	newItem.CFrame = Character:WaitForChild("HumanoidRootPart").CFrame + Character:WaitForChild("HumanoidRootPart").CFrame.LookVector * Vector3.new(2.5, 2.5, 2.5)
		wait(.01)
		
	end
	
	newItem:Destroy()
	
end)

script inside the hitbox part:

local db = false
local attacker = script.Parent.Name

function onTouched(hit)
	
	local human = hit.Parent:findFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if (human ~= nil) and db == false and hit.Name ~= (""..attacker.."") then
		
		print(""..attacker.."")
			
		db = true
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(4)
		game.ReplicatedStorage.RemoteEvents.Stunned:FireClient(player)
		
		wait(.2)
		
		db = false
		
	end
	
end

script.Parent.Touched:connect(onTouched)
1 Like

local db = false
local attacker = script.Parent.Name

function onTouched(hit)
	
	local human = hit.Parent:findFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if (human ~= nil) and db == false then
if hit.Parent.Name == attacker then
else
		
		print(""..attacker.."")
			
		db = true
		hit.Parent:FindFirstChild("Humanoid"):TakeDamage(4)
		game.ReplicatedStorage.RemoteEvents.Stunned:FireClient(player)
		
		wait(.2)
		
		db = false
		
	end
	end
end

script.Parent.Touched:connect(onTouched)

edited script a bit

There’s a few mistakes(?) in your code.

First, you shouldn’t be spawning a part with a script in it. Instead, you should have your code connected to the part directly in the remote event script. This would make the hitbox easily customizable.

Example:

script.Parent.OnServerEvent:Connect(function(Player)
	local Hitbox = Instance.new("Part", game:GetService("Workspace"))
	Hitbox.Material = Enum.Material.Neon
	Hitbox.BrickColor = BrickColor.new("Really red")
	Hitbox.Transparency = 0.75
	Hitbox.CanCollide = false
	Hitbox.Massless = true

	local Weld = Instance.new("Weld", Hitbox)
	Weld.Part0 = Hitbox
	Weld.Part1 = Player.Character.Torso

	Weld.C0 = Hitbox.CFrame * Vector3.new(0,0,1)

	local Damage = 4

	local OnCooldown = false

	Hitbox.Touched:Connect(function(Hit)
		if not Hit:IsDescendantOf(Player.Character) and Hit and Hit.Parent then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			if Humanoid then
				if OnCooldown == false then
					OnCooldown = true
					Humanoid:TakeDamage(Damage)

				end
			end
		end
	end)
end)

And you should probably use Connect(function(hit) instead of connect(function)
Also this code is a little messy in general, but mine is also pretty messy.
Try:

local db = false
local attacker = script.Parent.Name

script.Parent.Touched:Connect(function(hit)	
	local human = hit.Parent:findFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if (human ~= nil) and db == false then
		if not hit:IsDescendantOf(attacker) and hit and hit.Parent then

			print(""..attacker.."")

			db = true
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(4)
			game.ReplicatedStorage.RemoteEvents.Stunned:FireClient(player)

			wait(.2)

			db = false

		end
	end
end)
1 Like

so how do i get the script into the part if im spawning the part through a script?

I don’t understand the full context after reading through, but you can just store the script somewhere like ServerStorage and then :Clone() it when you need a copy.

1 Like

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