My HitBox does not do any damage

I am scripting a quick little hitbox script and I tested it out and it does not do damage to other players

local db = false
local attackDB = false
game.ReplicatedStorage.M1RE.OnServerEvent:Connect(function(plr)
	local SwordM1Track = plr.Character.Humanoid:LoadAnimation(plr.Character.Sword.SwordFunction.SwordM1) 
	local SwordValues = require(script.SwordValues)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hrp = char:WaitForChild("HumanoidRootPart")
	local HitBox = hrp:WaitForChild("HitBox")
	if db then return end
	
	
	if plr and not db then
	db = true
	SwordM1Track:Play()
	HitBox.Size = Vector3.new(10,10,10)
	HitBox.CanTouch = true
	HitBox.Touched:Connect(function(hit)
			local attackPlr = game.Players:GetPlayerFromCharacter(hit.Parent)

			for i,v in game.Workspace:GetChildren() do
				if v == plr then
					continue
				elseif attackPlr then
					attackPlr.Character:WaitForChild("Humanoid").Health = 0

				end
			end
	end)
	task.wait(SwordValues.AttackCooldown)
	SwordM1Track:Stop()
		HitBox.Size = Vector3.new(7,8,5)
		HitBox.CanTouch = false
		db = false	
		attackDB = false
	end	
	
end)

https://gyazo.com/fc8783df3167b61db9ebf5c0a5fa1139

2 Likes

The first major issue I see is that you’re comparing the player’s character to the player itself in the if statement “if v == plr then”. To fix this, just change the if statement to “if v.Name == plr.Name”.

1 Like

Ok I changed is there any other issues

1 Like

Do you see any errors in the output when you run your script? If so, list them

2 Likes

There is absolutely no errors in the output

2 Likes

I don’t see an end statement that ends the if statement “if plr and not db then”. Could you look into this?

3 Likes

Yeah I checked there is an if statement

2 Likes

I don’t see an end that ends that particular if statement, this can definitely lead to issues.

2 Likes

The end statement is at line 35

2 Likes

okay, first of all

please refrain from using WaitForChild inside remote event scripts, because if that child doesn’t exist, the code will yield and it won’t stop running, essentially exploiters can abuse this and fire the remote event multiple times and crash the server

second of all, why are you loading AND playing the animation on the server? why not just do it on the client?

third of all, why do you check if db == false in the if plr and not db then?
the function will stop if db is true the moment it hits the if db then return end line

fourth, instead of having a .Touched event, just use a for loop, because i assume this script runs each time the player swings a weapon

for _,v in Hitbox do
  local otherPlayer = game.Players:GetPlayerFromCharacter(v.Parent)

  if v.Parent:FindFirstChild("Humanoid") and otherPlayer then
   local enemy = v.Parent:FindFirstChild("Humanoid")
   enemy:TakeDamage(100) -- or enemy.Health = 0
  end
end
1 Like

I am playing the animations on a server script so everyone can see the animation

2 Likes

playing the animations on the client instantly replicates them to the server

1 Like

Oh ok so can you tell me why I get this error

ServerScriptService.ServerScriptService:18: attempt to iterate over a Instance value

1 Like

I don’t think I can loop through a part

1 Like

oh, my bad

local Size = Vector.new(10,10,10)
local CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame -- change this if you want to

local Hitbox = workspace:GetPartBoundsInBox(CFrame, Size)

for _,v in Hitbox do
  local otherPlayer = game.Players:GetPlayerFromCharacter(v.Parent)

  if v.Parent:FindFirstChild("Humanoid") and otherPlayer then
   local enemy = v.Parent:FindFirstChild("Humanoid")
   enemy:TakeDamage(100) -- or enemy.Health = 0
  end
end
1 Like

There is a couple of problems with this code so lemme fix it and I will tell you if it works

Have you tried using the debugging tool? You can find information on it here.

Though the issue may have to do with the type of hitbox you’re using. Touched() is unreliable and bad at detecting anything already inside the area. Instead, you should use a magnitude hitbox. I have a guide on how to use them here.