Weapon randomly hits people (Problem)

It randomly hits people in multiplayer when no one else is near. Can anyone explain? From random directions as well. Im guessing its to do with hitbox or lag but im not sure. Weapon is a sword model attached to player via motor 6d. Cant really explain more.

local function stopSwing() 
--occurs when swing stops
	task.wait(.2)
	swingCheck = false
	found = false
end
function hit(hitbox, character)
--check if someone in hitbox
	local Hit = {}
	while found == false and hitbox do
		wait(.1)
		local Enemy = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size) -- get everypart inside a box (In this case the hitbox we just create, I told you it will be easier this way)
		for i, v in pairs(Enemy) do -- run a loop through the part that is inside the box
			if not hitbox then break end
			if swingCheck then break end
			if found then break end
			local Char = v.Parent
			if not Char then continue end -- exclude itself
			if Char == character then continue end
			if character:FindFirstChild("IsRagdoll").Value == true then continue end
			if table.find(Hit, Char) then continue end -- check if the character is already been hit
			local Hum = Char:FindFirstChild("Humanoid")
			if not Hum then continue end -- check if it has humanoid
			if Hum.Health <= 0 then continue end -- don't hit the dead thing
			local Player = game.Players:GetPlayerFromCharacter(Char)
			if not Player then continue end -- this is unnesessary I just don't want the npc to hit other thing than player#
			if Char:FindFirstChild("IsRagdoll").Value == true then continue end
			found = true
			table.insert(Hit, Char)
			Hum:TakeDamage(12 + damageBoost)
			swordRagdoll(v.Parent, character)
			hitbox:Destroy()

			-- deal damage

 -- insert the character that has been hit into the table for check later
		end
	end
end
function newHitBox(character, playAnim)
-- make the hitbox
	local physicalBox = Instance.new("Part", character)
	physicalBox.Name = "Hitbox"
	physicalBox.Transparency = .6
	physicalBox.Anchored = false
	physicalBox.CanCollide = false
	physicalBox.Size = Vector3.new(5.5, 2.5, 5.5)
	physicalBox.Massless = true
	physicalBox.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0, 0, -3.5)
	physicalBox.Parent = character.HumanoidRootPart
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = character.HumanoidRootPart
	weld.Part1 = physicalBox
	weld.Parent = character.HumanoidRootPart
	game.Debris:AddItem(weld, 0.5)
	game.Debris:AddItem(physicalBox, 0.5)
	playAnim.Stopped:Connect(function()
-- destroy when swing ends
		physicalBox:Destroy()
	end)
	hit(physicalBox, character)
end
script.Parent.SwordCombatRemote.OnServerEvent:Connect(function(player)
-- first thing that happens on a click. play anims and stuff
	if player.Character.IsRagdoll.Value == true then return end
	if player.Character:WaitForChild("Humanoid").Health <= 0 then return end
	print("got here")
	print(swingCheck)
	if swingCheck then return end
	swingCheck = true
	local character = player.Character
	if character.IsRagdoll.Value then
		swingCheck = false
		return
	end
	local Hum = character:WaitForChild("Humanoid")
	if Combo == 1 then
		playAnim = Hum:LoadAnimation(anim)
		playAnim:Play()
		Combo = 2
	elseif Combo == 2 then
		playAnim = Hum:LoadAnimation(anim2)
		playAnim:Play()
		Combo = 1
	end
	playAnim.Stopped:Connect(function()
		stopSwing()
	end)
	task.wait(0.2)
	newHitBox(character, playAnim)
	sound:Play()
end)

2 Likes

you can’t weld together 2 unanchored parts with a weld constraint if I remember correctly, so the hitbox isn’t anchored and the weld is inactive

1 Like

you can weld 2 unanchored parts with a weldconstraint

but then why is my code not working

More details are need to really assess what’s going on here. Is it like, you hit one person but another recieves the damage? Or are people getting hit when nowhere close to the actual hitbox?

Also, kind of unrelated, but I’d reccomend swapping out the weld for an AlignPosition on max settings. Makes it so you don’t have to worry about anything effecting the hitbox physics-wise.