Hitbox doing way more damage than supposed to

Hello! My hitbox is doing much more damage than it is supposed to. I suspect it has something to do with the table of values in the hitbox, but I’m not completely sure.

here is the script:

local event = game.ReplicatedStorage.fightEvent

event.OnServerEvent:Connect(function(plr,isusingy)
	if isusingy == true then
		local hitbox = game.ReplicatedStorage.hitbox:Clone()
		hitbox.Parent = workspace
		local char = plr.Character
		hitbox.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(0,0,-5) * char.HumanoidRootPart.CFrame.LookVector * -1
		local ptsinhitbox = workspace:GetPartsInPart(hitbox)
		for i,v in pairs(ptsinhitbox) do
			local succ, err = pcall(function()
				if v.Parent.Humanoid then
					if v.Parent.Name ~= plr.Name then
						print("done")
						v.Parent.Humanoid.Health -= 5
						 
						print(v.Parent.Humanoid.Health)
					end
				end
			end)
			if not succ then
				print(err)
			end
		end
		wait(0.5)
		hitbox:Destroy()

	end
end)
2 Likes

Add a debounce instance to the humanoid named debounce, and if it has debounce, don’t damage that humanoid. Add the debouce instance to the games debris service for like 3 seconds or somethig.

I suppose that your hitbox consists of multiple parts. And the in pairs loop goes through all the parts and gives the player as much damage as the amount of parts found inside the hitbox. (For example there are 4 parts found then it gives the player 20 damage)

I think the solution is to combine the parts into one part or only damaging the player one time. I am still learning scripting so apologies if I couldn’t help you exactly.

This is now what my script looks like:

local event = game.ReplicatedStorage.fightEvent

event.OnServerEvent:Connect(function(plr,isusingy)
	if isusingy == true then
		local deb = false
		local hitbox = game.ReplicatedStorage.hitbox:Clone()
		hitbox.Parent = workspace
		local char = plr.Character
		hitbox.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(0,0,-5) * char.HumanoidRootPart.CFrame.LookVector * -1
		local ptsinhitbox = workspace:GetPartsInPart(hitbox)
		for i,v in pairs(ptsinhitbox) do
			local succ, err = pcall(function()
				if v.Parent.Humanoid then
					if v.Parent.Name ~= plr.Name and deb == false then
						print("done")
						v.Parent.Humanoid.Health -= 5
						deb = true
						hitbox:Destroy()
						wait(0.5)
						deb = false
						print(v.Parent.Humanoid.Health)
					end
				end
			end)
			if not succ then
				print(err)
			end
		end
		hitbox:Destroy()

	end
end)

the problem now is that when hit the other character once, it does damage multiple times instead of one time, but in intervals

1 Like

I meant a literal debouce instance

to Check
if v.Parent.Humanoid:FindFirstChild("debounce") then return end

To insert

local db = Instance.New("BoolValue")
db.Name = "debounce"
db.Parent = v.Parent.Humanoid
game.Debris:AddItem(db,3)

now it’s back to doing huge amounts of damage at once

It looks like you have multiple parts inside your hitbox. If you run a for in pairs loop it will go through the parts of the hitbox. Imagine you have 3 parts of your hitbox. Then it will grant you 5 damage 3 times because it loops through all instances that exist in that location.

How can I go about fixing this?

You must somehow make it one instance. Like combining all the parts in one. I am thinking about a solution too.

You must make the damage run only one time. I suppose there is a solution for this but I am trying to find out too.

This might help: Debounce Patterns | Documentation - Roblox Creator Hub

I’ll keep on finding more solutions.

You could do something like this. Check if you haven’t already damaged the humanoid by seeing if it’s found in the “Already Damaged” table. If it’s not then add the humanoid to it and proceed with your script.

local AlreadyDamaged = {}

for i,v in pairs(Parts) do
	local Humanoid = v.Parent and v.Parent:FindFirstChild("Humanoid")
	if not Humanoid then return end
	
	if table.find(AlreadyDamaged,Humanoid) then return end
	
	table.insert(AlreadyDamaged,Humanoid)
	
	--Do your stuff
end

I wish I could’ve test my solutions on my pc but try this post.

Try embedding the damage function inside it and disconnect it once run.

1 Like

sorry for the late response but this is the new script:

local event = game.ReplicatedStorage.fightEvent

event.OnServerEvent:Connect(function(plr,isusingy)
	if isusingy == true then

		local hitbox = game.ReplicatedStorage.hitbox:Clone()
		hitbox.Parent = workspace
		local char = plr.Character
		hitbox.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(0,0,-5) * char.HumanoidRootPart.CFrame.LookVector * -1
		local Alreadydam = {}
		local ptsinhitbox = workspace:GetPartsInPart(hitbox)
		 for i,v in pairs(ptsinhitbox) do
			local succ, err = pcall(function()
				if v.Parent.Humanoid then
					if v.Parent.Name ~= plr.Name then
						if table.find(Alreadydam, v.Parent.Humanoid) then return end
						table.insert(Alreadydam, v.Parent.Humanoid)
						
						print("done")
						
						v.Parent.Humanoid.Health -= 5
						
						hitbox:Destroy()
						
						wait(0.5)
						table.remove(Alreadydam, table.find(Alreadydam, v.Parent.Humanoid))
					end
				end
			end)
		end
		hitbox:Destroy()

	end
end)

Now its doing large amounts of damage at once but in intervals.

I ended up using a Touched event and it works fine. I know this isn’t the best method, but it’s working well for me so I’m going to use it for now. Thank you all for your help, though.

2 Likes

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