Problems with Hitbox

Ok, I changed it to where it uses only one script.

ChargeTool.rbxl (57.8 KB)

image

local tool = script.Parent
local character = nil
local humanoid = nil
local hrp = nil
local hitbox = nil
local sound = nil
local hitsound = nil
local hitEvent = nil
local isCharging = false
local cooldown = false
local Force = nil

local damageAmount = 15

function StopCharge()
	isCharging = false
	sound:Stop()
	if Force then
		Force:Destroy()
		Force =  nil
	end
end

function DoHit(part)
	if isCharging then
		if part.Parent:FindFirstChild("Humanoid") then
			local targetChar = part.Parent
			if targetChar.Name ~= character.Name then
				StopCharge()
				hitsound:Play()
				print(targetChar)
				local targetHum = targetChar:FindFirstChild("Humanoid")
				if targetHum then
					targetHum:TakeDamage(damageAmount)
				end	
			end
		else
			StopCharge()
			hitsound:Play()
			print(part.Name)
		end 
	end
end


tool.Equipped:Connect(function()
	character = tool.Parent
	humanoid = character:WaitForChild("Humanoid")
	hrp = character.PrimaryPart	
	
	--create a hitbox

	if character:FindFirstChild("Hitbox") then
		character.Hitbox:Destroy()
	end

	hitbox = Instance.new('Part')
	hitbox.Transparency = 0.7
	hitbox.CanCollide = false
	hitbox.Name = "Hitbox"
	hitbox.Size = Vector3.new(4,4,4)
	hitbox.CFrame = hrp.CFrame * CFrame.new(0, 0, 0)
	hitbox.Massless = true
	local weld = Instance.new("WeldConstraint")
	weld.Part1 = hitbox
	weld.Part0 = hrp
	weld.Parent = hitbox
	hitbox.Parent = character
	
	hitEvent = hitbox.Touched:Connect(DoHit)
	
	--create sounds
	sound = Instance.new('Sound')
	sound.SoundId = "http://www.roblox.com/asset/?id=111896685"
	sound.Parent = tool.Parent.Head

	hitsound = Instance.new('Sound')
	hitsound.SoundId = "rbxassetid://9118612665"
	hitsound.Parent = tool.Parent.HumanoidRootPart


end)

tool.Unequipped:Connect(function()
	if Force then
		Force:Destroy()
		Force = nil
	end
	if hitEvent then
		hitEvent:Disconnect()
		hitEvent = nil
	end	
	if hitbox then
		hitbox:Destroy()
		hitbox = nil
	end
	if sound then 
		sound:Destroy()
		sound = nil
	end
	if hitsound then
		hitsound:Destroy()
		hitsound = nil
	end
end)




tool.Activated:Connect(function()
	if not cooldown then
		cooldown = true
		isCharging = true
		sound:Play()

		if Force then 
			Force:Destroy()
		end
		Force = Instance.new("BodyVelocity") 
		Force.maxForce = Vector3.new(88800, 0, 88800)
		Force.Parent = hrp
		Force.Velocity = hrp.CFrame.LookVector*60
		
		humanoid.AutoRotate = false
--		humanoid.WalkSpeed = 0 -- temp
		task.wait(2)
		StopCharge()
		humanoid.AutoRotate = true
--		humanoid.WalkSpeed = 16 -- temp

		task.wait(5)
		cooldown = false
	end
end)

why not change it to

elseif part.Parent:FindFirstChild("Humanoid") == nil then

.Touched for hitboxes…

it’s SO unreliable :sob:
literally just do GetPartBoundsInBox or GetPartsInPart

to get the character from that:

for _, something: BasePart in detectionBox do
   local character = something.Parent
   local player = players:GetPlayerFromCharacter(character) -- verify that it's a player

   if not player then continue end -- if it's not a player, check the next part
end
1 Like

If any of the posts here solved your problem, or led to a solution, you should probably mark this as ‘solved’

I’m sorry :sob:

I’m quite obviously not talented at programming, so expect stuff like this to occur.

This appears to be working as it seems, just changed a little bit of the code. Thank you!

1 Like