How to make my code repeat less

My code repeat a lot. how to make it less repeative?

	local NewHitbox1 = Hitbox:Clone()
	NewHitbox1.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-4) 
	
	local NewHitbox2 = Hitbox:Clone()
	NewHitbox2.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(2,2,-5) 
	
	local NewHitbox3 = Hitbox:Clone()
	NewHitbox3.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(-2,-1,-4) 
	
	local NewHitbox4 = Hitbox:Clone()
	NewHitbox4.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(-1,-2,-5) 
	
	local NewHitbox5 = Hitbox:Clone()
	NewHitbox5.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(1,-2.5,-5) 
	
	NewHitbox1.Parent = workspace
	wait(waitingTime)
	NewHitbox2.Parent = workspace
	wait(waitingTime)
	NewHitbox3.Parent = workspace
	wait(waitingTime)
	NewHitbox4.Parent = workspace
	wait(waitingTime)
	NewHitbox5.Parent = workspace
	
	wait(2)
	
	NewHitbox1:Destroy()
	wait(waitingTime)
	NewHitbox2:Destroy()
	wait(waitingTime)
	NewHitbox3:Destroy()
	wait(waitingTime)
	NewHitbox4:Destroy()
	wait(waitingTime)
	NewHitbox5:Destroy()
1 Like

What do u want to repeat less exactly?

basically all of the above. i want it to only write a code one time but not 5 time

1 Like

Then why did u do it 5 times? Just remove the lines:

local NewHitbox1 = Hitbox:Clone()
	NewHitbox1.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-4) 
	
	local NewHitbox2 = Hitbox:Clone()
	NewHitbox2.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(2,2,-5) 
	
	local NewHitbox3 = Hitbox:Clone()
	NewHitbox3.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(-2,-1,-4) 
	
	local NewHitbox4 = Hitbox:Clone()
	NewHitbox4.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(-1,-2,-5) 
	
	local NewHitbox5 = Hitbox:Clone()
	NewHitbox5.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(1,-2.5,-5) 
	
	NewHitbox1.Parent = workspace
	
	wait(2)
	
	NewHitbox1:Destroy()

nooo, i want all of the things destroyed and all of the things parent set to workspace

Do you want to make your code better or what do you want to do?

make my code more efficient and better

I will send u the code then now

for i = 1, 5, 1 do
    local _ : Part = Hitbox:Clone()
    _.CFrame = char.PrimaryPart.CFrame * CFrame.new(0, 0, -4)
    _.Parent = game.Workspace
    task.wait(waitingTime + 2)
    _:Destroy()
end
local hitboxTable = {0,0,-4, 2,2,-5, -2,-1,-4, -1,-2,-5, 1,-2.5,-5}

local function hitBoxFunction(value)
	
	local clonedHitBox = Hitbox:Clone()

	clonedHitBox.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(value)
	
	clonedHitBox.Parent = workspace
	
	task.wait(2)
	
	clonedHitBox:Destroy()
	
end

for index, value in pairs(hitboxTable) do
	
	task.spawn(hitBoxFunction, value)
	
	task.wait(waitingTime)
	
end

Im not sure if this works, test it. If u add another CFrame to the hitboxTable it will repeat more or less when u remove CFrames on the Table.

got the error ServerScriptService.iryugiriScript:16: invalid argument #1 to β€˜new’ (Vector3 expected, got number)

local HitboxLocations = {
    CFrame.new(0,0,-4),
    CFrame.new(2,2,-5),
    CFrame.new(-2,-1,-4), 
    CFrame.new(-1,-2,-5), 
    CFrame.new(1,-2.5,-5)
}

local Hitboxes = {}

for _, hitboxLocation: CFrame in HitboxLocations do
    local hitbox = Hitbox:Clone()
    hitbox.CFrame = hitboxLocation
    table.insert(Hitboxes, hitbox)
end

for _, hitbox in Hitboxes do
    hitbox.Parent = workspace
    task.delay(waitingTime + 2, hitbox.Destroy, hitbox)
end

got the error ServerScriptService.iryugiriScript:20: attempt to iterate over a nil value

Sorry it was a typo, fixed it in original reply.

local hitboxTable = {CFrame.new(0,0,-4), CFrame.new(2,2,-5), CFrame.new(-2,-1,-4), CFrame.new(-1,-2,-5), CFrame.new(1,-2.5,-5)}

local function hitBoxFunction(value)
	
	local clonedHitBox = Hitbox:Clone()

	clonedHitBox.CFrame = char.HumanoidRootPart.CFrame * value
	
	clonedHitBox.Parent = workspace
	
	task.wait(2)
	
	clonedHitBox:Destroy()
	
end

for index, value in pairs(hitboxTable) do
	
	task.spawn(hitBoxFunction, value)
	
	task.wait(waitingTime)
	
end

Do this

1 Like

can u explain how this script works?

The pairs loops through all CFrames in the hitboxTable. Every time when it loops, it fires a Function with the value inside the hitBoxTable but im firing the hitBoxFunction with task.spawn so it dont waits 2 seconds and fires instantly another funtion after the waitingTime is done. Now inside the hitBoxFunction it spawns a hitBox with the value and destroys it after 2 seconds.

1 Like

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