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)
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.
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.