Is there any way to make this combat system hit only once?

Basically, I’m making a combat system but I have encountered a particularly annoying problem with it, it hits the target MULTIPLE times when I only want it to hit them once.

Is there any way to make this combat system hit only once? It uses a for loop and workspace:GetPartsBoundInBox(), by the way!

My attempt at only making it hit once was to add a break at the end of the for loop, though that only makes it hit less often, for some reason.

Here’s a demonstration of what the code looks like, I just removed everything that was miscellaneous inside of the if statement, which was funnily enough everything:

game:GetService("ReplicatedStorage").Remotes.Combat.Damage.OnServerEvent:Connect(function(player, MoveInfo : {})
	local char = player.Character or player.CharacterAdded:Wait() or workspace:FindFirstChild(player.Name, true)

	local params = OverlapParams.new()
	params.FilterDescendantsInstances = {char}
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local hitbox = workspace:GetPartBoundsInBox(MoveInfo["CFrame"] or char:FindFirstChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -3.5), MoveInfo["Size"] or Vector3.new(4, 5, 4), params)

	for Index, Object in pairs(hitbox) do
		if Object.Parent ~= char and Object.Parent:FindFirstChild("Humanoid") then
             
        end
		break -- Here's what I tried to do in order for it to hit once, this doesn't work sadly.
	end
end)
2 Likes

Use return instead of break.

2 Likes

Usually when I make a combat system, if I plan to only have the attack hit one character at a time, I have a value that checks if it’s already done damage. However, if I have multiple, I use a table to store all the characters/enemies hit so far (Their model instance) and then loop through that table whenever a new hit lands to check if they’re already in there.

1 Like

So do I just replace the break in the script with return?

1 Like

Yep, if I’m not wrong that is.

1 Like

blacklist table, when someone gets hit, table.insert to the blacklist table the hits parent. then check if the hits parent is in the table.

1 Like

I did something with return and that seemed to work, thank you for pointing me to this in the first place!

(If I use tables I might actually get a headache, it just seems so confusing to me.)

Tables is a better solution. Also you are using a damage remote?..

Yeah, is there an exploit for damage remotes or something?

Doing a move remote would be better than a damage remote…

I do have move remotes in the game as well, I just think having 1 remote for damaging would be a bit more organized, most of the move remotes I have in my game are just for effects and other miscellaneous things.

1 Like

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