How to add the enemies to the table just one time with the touched event?

ok i’ll try it, i hope it works

but i still think the problem is within ur hitbox.onTouch, because ur current script will cause it to Grab the first one it can read, rather than the closest one, if u want, it mighttake some time but i can rewrite some part of ur script, so it collects the enemy within first, before it loops to the “hitedPeople”

so you would rewrite the script without my hitbox if i understand (english is not my first language)

kinda, but il probably just create a function that gets all the enemy within the hitbox, so u just use that function and it will return array of all the enemies within the hitbox, that way, u can loop through them all to find the distance first before grabbing anyone, because ur hitbox.onTouch will trigger the grab the first time it reads an enemy (doesnt matter if closer or farther)

doesn’t this take a lot of work for you? I’m feeling kind bad

try this, let me know the result, take a video of the behaviour along with the output if possible

local grabRemote = RP.remotes.grab
local ss = game:GetService("ServerStorage")
local modules = ss:WaitForChild("Modules")
local slow = require(modules:WaitForChild("slow"))
local runService = game:GetService("RunService")
local rockModule = require(modules:WaitForChild("RockModule"))
local utilities = require(modules:WaitForChild("particleEmitter"))
local hitbox = require(modules:WaitForChild("hitbox"))
local hitService = require(modules:WaitForChild("hitService"))

local hitedPeople = {}
local grabAnins = {}

local looping = false

local startTick
local weld
local enemy
local enemyHumRP

local TheTarget = nil
local TheTargetRP = nil



local function aplyVelocity(targetHumRP,direction)
	local linearV = Instance.new("LinearVelocity",targetHumRP)
	local attach = targetHumRP.RootAttachment
	linearV.MaxForce = 100000
	linearV.ForceLimitMode = Enum.ForceLimitMode.PerAxis
	linearV.MaxAxesForce = Vector3.new(100000,0,100000)
	linearV.VectorVelocity = direction
	linearV.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
	linearV.Attachment0 = attach
	linearV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
	attach.WorldPosition = targetHumRP.AssemblyCenterOfMass
	linearV.ForceLimitsEnabled = true
	game.Debris:AddItem(linearV,0.2)
end

local function GetAllEnemiesWithinHitbox(Hitbox, MyHumanoid)
	local AllEnemies = {}
	local CurrentDistance = math.huge
	local CurrentTarget = nil
	local CurrentTargetHRP = nil
	
	local Array = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size)
	
	if Array ~= nil then
		for i, v in Array do
			local Model = v:FindFirstAncestorOfClass("Model")
			if Model then
				local Hum = Model:FindFirstChild("Humanoid")
				if Hum and Hum ~= MyHumanoid then
					
					local Dist = (Model.PrimaryPart.Position - MyHumanoid.Parent.PrimaryPart.Position).Magnitude
					
					if Dist < CurrentDistance then
						CurrentTarget = Model
						CurrentDistance = Dist
						CurrentTargetHRP = Model:FindFirstChild("HumanoidRootPart")
					end
					
				end
			end
		end
	end
	
	return CurrentTarget, CurrentTargetHRP
	
end

grabRemote.OnServerEvent:Connect(function(plr,grab,input)	
	local character = plr.Character
	local humanoid = character:WaitForChild("Humanoid")
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

	local grabing = character:GetAttribute("Grab")
	local attacking = character:GetAttribute("Attacking")
	local punching = character:GetAttribute("Punching")
	local blocking = character:GetAttribute("Blocking")
	local jumping = character:GetAttribute("Jumping")
	local slowed = character:GetAttribute("Slowed")
	local stunned = character:GetAttribute("Stunned")
	local dashing = character:GetAttribute("Dash")
	local dashing2 = character:GetAttribute("Dash2")

	if grab then
		if grabing or attacking or punching or blocking or stunned or dashing or dashing2 then return end

		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=85032147152709"
		local animationTrack = humanoid:LoadAnimation(animation)

		local animationGrabing = Instance.new("Animation")
		animationGrabing.AnimationId = "http://www.roblox.com/asset/?id=anim"
		local animationTrackGrabing = humanoid:LoadAnimation(animationGrabing)

		local function GrabFunc()
			local hitbox = Instance.new("Part")
			hitbox.Name = "HitBox"
			hitbox.Transparency = 1
			hitbox.CanCollide = false
			hitbox.Anchored = true
			hitbox.Size = Vector3.new(14,14,14)
			hitbox.CFrame = humanoidRootPart
			hitbox.Offset = CFrame.new(0,0,-2)
			hitbox.Visualizer = true

			local Target, TargetHRP = GetAllEnemiesWithinHitbox(hitbox)

			if Target ~= nil then
				
				TheTarget = Target
				TheTargetRP = TargetHRP
				
				looping = true
				character:SetAttribute("Grab",true)
				enemy = Target
				enemyHumRP = TargetHRP

				for i,v in pairs(Target:GetChildren()) do
					if v:IsA("BasePart") then
						v.Color = Color3.fromRGB(0,100,80)
					end
				end

				weld = Instance.new("Weld")
				weld.C1 = CFrame.new(0,0,-2) * CFrame.Angles(0,math.rad(180),0)
				weld.Part0 = TargetHRP
				weld.Part1 = humanoidRootPart
				weld.Name = "wellwellwell"

				local knockback = nil
				local maxForce = nil

				startTick = tick()

				while looping do
					humanoid.WalkSpeed = 10
					humanoid.JumpPower = 0

					weld.Parent = TargetHRP
					Target.Humanoid.PlatformStand = true


					if tick() - startTick > 3 then			
						print(Target,TargetHRP)
						character:SetAttribute("Grab",false)
						Target.Humanoid.PlatformStand = false
						humanoid.WalkSpeed = 20
						humanoid.JumpPower = 50

						for i,v in pairs(TargetHRP:GetChildren()) do
							if v:IsA("Weld") and v.Name == "wellwellwell" then
								v.Parent = nil
							end
						end


						aplyVelocity(TargetHRP,Vector3.new(0,0,30))

						break
					end

					task.wait()
				end

				hitService.hit(Target.Humanoid,nil,knockback,nil,character,nil,nil,maxForce,nil,nil)

			end	
		end
		


		animationTrack.KeyframeReached:Connect(function(kf)
			if kf == "grab" then
				GrabFunc()
			end
		end)

		animationTrack:Play()
		grabAnins[plr] = animationTrack
	else
		if grabing then				
			looping = false
			character:SetAttribute("Grab",false)
			grabAnins[plr]:Stop()
			grabAnins[plr] = nil

			humanoid.WalkSpeed = 20
			humanoid.JumpPower = 50

			TheTarget.Humanoid.PlatformStand = false

			for i,v in pairs(TheTargetRP:GetChildren()) do
				if v:IsA("Weld") and v.Name == "wellwellwell" then
					v.Enabled = false
				end
			end

			if tick() - startTick < 3 and input == Enum.KeyCode.T then
				aplyVelocity(TheTargetRP,Vector3.new(0,0,30))
			elseif input == Enum.KeyCode.Y then
				aplyVelocity(TheTargetRP,Vector3.new(30,0,0))
			elseif input == Enum.KeyCode.G then
				aplyVelocity(TheTargetRP,Vector3.new(-30,0,0))
			end			

			hitedPeople = {}
		end

	end	
end)

the video is quick, because roblox just accept 10BM, but there is what is rappening

wheres the video? does it output anything?

you are not seeing the video? gggggggggggg

nope none, u should use the Upload button when ure going to reply, it will open file explorer and u add the file

its up yout code jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj

im asking after u use this new code

local grabRemote = RP.remotes.grab
local ss = game:GetService("ServerStorage")
local modules = ss:WaitForChild("Modules")
local slow = require(modules:WaitForChild("slow"))
local runService = game:GetService("RunService")
local rockModule = require(modules:WaitForChild("RockModule"))
local utilities = require(modules:WaitForChild("particleEmitter"))
local hitbox = require(modules:WaitForChild("hitbox"))
local hitService = require(modules:WaitForChild("hitService"))

local hitedPeople = {}
local grabAnins = {}

local looping = false

local startTick
local weld
local enemy
local enemyHumRP

local TheTarget = nil
local TheTargetRP = nil



local function aplyVelocity(targetHumRP,direction)
	local linearV = Instance.new("LinearVelocity",targetHumRP)
	local attach = targetHumRP.RootAttachment
	linearV.MaxForce = 100000
	linearV.ForceLimitMode = Enum.ForceLimitMode.PerAxis
	linearV.MaxAxesForce = Vector3.new(100000,0,100000)
	linearV.VectorVelocity = direction
	linearV.VelocityConstraintMode = Enum.VelocityConstraintMode.Vector
	linearV.Attachment0 = attach
	linearV.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
	attach.WorldPosition = targetHumRP.AssemblyCenterOfMass
	linearV.ForceLimitsEnabled = true
	game.Debris:AddItem(linearV,0.2)
end

local function GetAllEnemiesWithinHitbox(Hitbox, MyHumanoid)
	local AllEnemies = {}
	local CurrentDistance = math.huge
	local CurrentTarget = nil
	local CurrentTargetHRP = nil
	
	local Array = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size)
	
	if Array ~= nil then
		for i, v in Array do
			local Model = v:FindFirstAncestorOfClass("Model")
			if Model then
				local Hum = Model:FindFirstChild("Humanoid")
				if Hum and Hum ~= MyHumanoid then
					
					local Dist = (Model.PrimaryPart.Position - MyHumanoid.Parent.PrimaryPart.Position).Magnitude
					
					if Dist < CurrentDistance then
						CurrentTarget = Model
						CurrentDistance = Dist
						CurrentTargetHRP = Model:FindFirstChild("HumanoidRootPart")
					end
					
				end
			end
		end
	end
	
	return CurrentTarget, CurrentTargetHRP
	
end

grabRemote.OnServerEvent:Connect(function(plr,grab,input)	
	local character = plr.Character
	local humanoid = character:WaitForChild("Humanoid")
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

	local grabing = character:GetAttribute("Grab")
	local attacking = character:GetAttribute("Attacking")
	local punching = character:GetAttribute("Punching")
	local blocking = character:GetAttribute("Blocking")
	local jumping = character:GetAttribute("Jumping")
	local slowed = character:GetAttribute("Slowed")
	local stunned = character:GetAttribute("Stunned")
	local dashing = character:GetAttribute("Dash")
	local dashing2 = character:GetAttribute("Dash2")

	if grab then
		if grabing or attacking or punching or blocking or stunned or dashing or dashing2 then return end

		local animation = Instance.new("Animation")
		animation.AnimationId = "http://www.roblox.com/asset/?id=85032147152709"
		local animationTrack = humanoid:LoadAnimation(animation)

		local animationGrabing = Instance.new("Animation")
		animationGrabing.AnimationId = "http://www.roblox.com/asset/?id=anim"
		local animationTrackGrabing = humanoid:LoadAnimation(animationGrabing)

		local function GrabFunc()
			local hitbox = Instance.new("Part")
			hitbox.Name = "HitBox"
			hitbox.Transparency = 1
			hitbox.CanCollide = false
			hitbox.Anchored = true
			hitbox.Size = Vector3.new(14,14,14)
			hitbox.CFrame = humanoidRootPart
			hitbox.Offset = CFrame.new(0,0,-2)
			hitbox.Visualizer = true

			local Target, TargetHRP = GetAllEnemiesWithinHitbox(hitbox)

			if Target ~= nil then
				
				TheTarget = Target
				TheTargetRP = TargetHRP
				
				looping = true
				character:SetAttribute("Grab",true)
				enemy = Target
				enemyHumRP = TargetHRP

				for i,v in pairs(Target:GetChildren()) do
					if v:IsA("BasePart") then
						v.Color = Color3.fromRGB(0,100,80)
					end
				end

				weld = Instance.new("Weld")
				weld.C1 = CFrame.new(0,0,-2) * CFrame.Angles(0,math.rad(180),0)
				weld.Part0 = TargetHRP
				weld.Part1 = humanoidRootPart
				weld.Name = "wellwellwell"

				local knockback = nil
				local maxForce = nil

				startTick = tick()

				while looping do
					humanoid.WalkSpeed = 10
					humanoid.JumpPower = 0

					weld.Parent = TargetHRP
					Target.Humanoid.PlatformStand = true


					if tick() - startTick > 3 then			
						print(Target,TargetHRP)
						character:SetAttribute("Grab",false)
						Target.Humanoid.PlatformStand = false
						humanoid.WalkSpeed = 20
						humanoid.JumpPower = 50

						for i,v in pairs(TargetHRP:GetChildren()) do
							if v:IsA("Weld") and v.Name == "wellwellwell" then
								v.Parent = nil
							end
						end


						aplyVelocity(TargetHRP,Vector3.new(0,0,30))

						break
					end

					task.wait()
				end

				hitService.hit(Target.Humanoid,nil,knockback,nil,character,nil,nil,maxForce,nil,nil)

			end	
		end
		


		animationTrack.KeyframeReached:Connect(function(kf)
			if kf == "grab" then
				GrabFunc()
			end
		end)

		animationTrack:Play()
		grabAnins[plr] = animationTrack
	else
		if grabing then				
			looping = false
			character:SetAttribute("Grab",false)
			grabAnins[plr]:Stop()
			grabAnins[plr] = nil

			humanoid.WalkSpeed = 20
			humanoid.JumpPower = 50

			TheTarget.Humanoid.PlatformStand = false

			for i,v in pairs(TheTargetRP:GetChildren()) do
				if v:IsA("Weld") and v.Name == "wellwellwell" then
					v.Enabled = false
				end
			end

			if tick() - startTick < 3 and input == Enum.KeyCode.T then
				aplyVelocity(TheTargetRP,Vector3.new(0,0,30))
			elseif input == Enum.KeyCode.Y then
				aplyVelocity(TheTargetRP,Vector3.new(30,0,0))
			elseif input == Enum.KeyCode.G then
				aplyVelocity(TheTargetRP,Vector3.new(-30,0,0))
			end			

			hitedPeople = {}
		end

	end	
end)

ok, one last question, these hitbox works well?

the hitbox i made

local function GetAllEnemiesWithinHitbox(Hitbox, MyHumanoid)

will automatically output all enemies within the hitbox cframe and size

ok, i think now it works sssssssssssssssss

u mean u got everything u wanted? can i see the video?

the code have multiple errors uiuiuiuiuiuiuiuiuiuiuiuiuiui

can u take a video and show me the errors?

ive noticed

			hitbox.CFrame = humanoidRootPart

change it to

			hitbox.CFrame = humanoidRootPart.CFrame