The Client/Server hitbox multiplying?

Howdy fellow users.
Today I’m facing an issue. It is the client/server hitboxes multiplying for no reason. Here’s the video

I really don’t know what’s happening…
and yes, the code makes me want to throw up.

Some of the codes aren’t finished.

Serverscript

local Tool = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Model = ReplicatedStorage["Item Clones"]:WaitForChild("Model1")

local RunService = game:GetService("RunService")
local loop

local HitboxServer = game.Workspace:WaitForChild("HitboxServer")

local Remotes = Tool:WaitForChild("Remotes")
local EquipEvent = Remotes:WaitForChild("Equip")
local AttackEvent = Remotes:WaitForChild("Attack")
local SkillEvent = Remotes:WaitForChild("Skill")

local SoundEffects = script:WaitForChild("SoundEffect")

local SwingSound = {
	-- Swing sound effect
	[1] = SoundEffects:WaitForChild("Swing1"),
	[2] = SoundEffects:WaitForChild("Swing2"),
}
local PunchSound = {
	-- Punch sound effect
	[1] = SoundEffects:WaitForChild("Punched1"),
	[2] = SoundEffects:WaitForChild("Punched2")
}

local Clone1,Clone2

local function EquipRemote(Player,Instruction,Character)
	if Instruction == "Equipping" then
		Clone1,Clone2 = Model:Clone(),Model:Clone()
		Clone1.Parent = Tool
		Clone2.Parent = Tool
	
	local Weld1,Weld2 = Instance.new("Weld"),Instance.new("Weld")
		Weld1.Parent = Character:WaitForChild("Right Arm")
		Weld2.Parent = Character:WaitForChild("Left Arm")
	
	-- Weld1
		Weld1.Part0 = Character:WaitForChild("Right Arm")
		Weld1.Part1 = Clone1
		Weld1.C1 = CFrame.new(0,0.78,0) * CFrame.Angles(0,math.rad(-180),0)
	
	--Weld2
		Weld2.Part0 = Character:WaitForChild("Left Arm")
		Weld2.Part1 = Clone2
		Weld2.C1 = CFrame.new(0,0.78,0)
	elseif Instruction == "Unequipping" then
		Clone1:Destroy()
		Clone2:Destroy()
	end
end

local function AttackRemote(Player,Instruction2,HitRegistrationBool,AttackBool,Hitbox,Damage,HitboxSize,HitboxPosition)
	if Instruction2 == "Attack" then
		local RaycastP = RaycastParams.new()
		RaycastP.FilterDescendantsInstances = {Player.Character, Player.Team}
		RaycastP.FilterType = Enum.RaycastFilterType.Exclude
		
		local ServerHitbox = Instance.new("Part")
		ServerHitbox.Anchored = true
		ServerHitbox.Parent = HitboxServer
		ServerHitbox.Size = HitboxSize
		ServerHitbox.Position = HitboxPosition
		ServerHitbox.CanCollide = false
		ServerHitbox.Transparency = 1
		
		local RandomSwingSound = SwingSound[math.random(1, #SwingSound)]
		RandomSwingSound.Parent = Player.Character:WaitForChild("HumanoidRootPart")
		RandomSwingSound:Play()
		loop = RunService.Heartbeat:Connect(function()
			local HitboxRayResult = workspace:Raycast(Player.Character.HumanoidRootPart.Position, ServerHitbox.Position, RaycastP)
			if HitboxRayResult and HitRegistrationBool then
				local InstanceResult = HitboxRayResult.Instance
				local Humanoid = InstanceResult.Parent:FindFirstChildWhichIsA("Humanoid")
				if Humanoid then
					Humanoid:TakeDamage(10)
					AttackEvent:FireClient(Player, "Detected")
				else
					AttackEvent:FireClient(Player, "NotDetected")
				end
				HitRegistrationBool = false
				loop:Disconnect()
			end
		end)
	end
end

EquipEvent.OnServerEvent:Connect(EquipRemote)
AttackEvent.OnServerEvent:Connect(AttackRemote)

LocalScript

local Tool = script.Parent

local EquipBool = false
local AttackBool = false
local HitRegistrationBool = false

local HitboxClientFolder = game.Workspace:WaitForChild("HitboxClients")

local Remotes = Tool:WaitForChild("Remotes")
local EquipEvent = Remotes:WaitForChild("Equip")
local AttackEvent = Remotes:WaitForChild("Attack")
local SkillEvent = Remotes:WaitForChild("Skill")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Configuration = Tool:WaitForChild("Configuration")
local Damage = Configuration:WaitForChild("Damage")
local Combo = Configuration:WaitForChild("Combo")
local Cooldown = Configuration:WaitForChild("Cooldown")
local ComboCooldown = Configuration:WaitForChild("ComboCooldown")

local Humanoid

local Animations = script:WaitForChild("Animations")
local Equip = Animations:WaitForChild("Equip")
local Idle = Animations:WaitForChild("Idle")
local Punch1 = Animations:WaitForChild("Punch1")
local Punch2 = Animations:WaitForChild("Punch2")

local EquipAnimation 
local IdleAnimation
local Punch1Animation
local Punch2Animation

local Hitbox

-- And here, we tolerate about the hitbox.
local HitboxSize = Vector3.new(4,5,3) -- x,y,z
local HitboxCFrame = CFrame.new(0,-0.5,-2) -- march march dabrowski

Tool.Equipped:Connect(function()
	EquipBool = true
	
	Humanoid = Character:WaitForChild("Humanoid")
	
	EquipEvent:FireServer("Equipping",Character)
	
	EquipAnimation = Humanoid:LoadAnimation(Equip)
	IdleAnimation = Humanoid:LoadAnimation(Idle)
	Punch1Animation = Humanoid:LoadAnimation(Punch1)
	Punch2Animation = Humanoid:LoadAnimation(Punch2)
	
	EquipAnimation:Play()
	EquipAnimation.Stopped:Wait()
	if EquipBool then
		IdleAnimation:Play()
	end
end)

Tool.Activated:Connect(function()
	if not AttackBool then
		AttackBool = true
		
		local AttackTable = {
			Punch1Animation,
			Punch2Animation
		}
		
		local RandomAnimationAttack = AttackTable[math.random(1, #AttackTable)]
		RandomAnimationAttack:Play()
		
		RandomAnimationAttack:GetMarkerReachedSignal("Hit_Registration"):Connect(function()
			HitRegistrationBool = true
			
			Hitbox = Instance.new("Part", HitboxClientFolder)
			Hitbox.Name = "ClientHitbox"
			Hitbox.Material = Enum.Material.ForceField
			Hitbox.Color = Color3.fromRGB(255, 0, 0)
			Hitbox.Anchored = true
			Hitbox.CanCollide = false
			Hitbox.Size = HitboxSize
			Hitbox.CFrame = Character.HumanoidRootPart.CFrame * HitboxCFrame
			
			local HitboxPosition = Hitbox.Position
				
			AttackEvent:FireServer("Attack",HitRegistrationBool,AttackBool,Hitbox,Damage,HitboxSize,HitboxPosition)
			RandomAnimationAttack.Stopped:Wait()
			HitRegistrationBool = false
		end)
		
		wait(Cooldown.Value)
		AttackBool = false
	end
end)

local function ClientEvent(instruction)
	if instruction == "Detected" then
		Hitbox.Color = Color3.fromRGB(0, 255, 0)
	elseif instruction == "NotDetected" then
		Hitbox.Color = Color3.fromRGB(255, 0, 0)
	end
end

Tool.Unequipped:Connect(function()
	EquipBool = false
	AttackBool = false
	HitRegistrationBool = false
	
	if EquipAnimation.IsPlaying then
		EquipAnimation:Stop()
	elseif IdleAnimation.IsPlaying then
		IdleAnimation:Stop()
	end
	
	EquipEvent:FireServer("Unequipping")
end)

AttackEvent.OnClientEvent:Connect(ClientEvent)

Thank you :3
I will check this tomorrow, or right now if I have a time :stuck_out_tongue:

1 Like

You have to disconnect this function, otherwise the events will stack and multiple hitboxes will be created. You’ve done this in the script before, so I’m assuming you know how to do this but forgot it for this one.

Not sure if there’s anything else, but that’s the first issue I found.

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