Any way to decrease length of this script?

local tile = script.Parent
local debounce = false

tile.Touched:Connect(function(hit)
if debounce then
return
end
if hit.Parent:FindFirstChild(“Humanoid”) then
debounce = true
local part = Instance.new(“Part”)
part.Parent = workspace
part.Position = tile.Position + Vector3.new(10,0,0)
local PartTwo = part:Clone()
PartTwo.Parent = workspace
PartTwo.Position = tile.Position + Vector3.new(-10, 0, 0)
local PartThree = part:Clone()
PartThree.Parent = workspace
PartThree.Position = tile.Position + Vector3.new(0, 0, 10)
local PartFour = part:Clone()
PartFour.Parent = workspace
PartFour.Position = tile.Position + Vector3.new(0, 0, -10)
end
end)

I feel like there would be a way to decrease this script using a loop or something.

You could do something like the following, which I haven’t tested. It is a bit cleaner and the number of parts can be increased by adding new positions to the table.

EDIT: More complete example based on OP

local tile = script.Parent
local debounce = false
local positions = {
  Vector3.new(10,0,0),
  Vector3.new(-10,0,0),
  Vector3.new(0,0,10),
  Vector3.new(0,0,-10)
}

local function handleTouched(hit)
  if debounce then
    return
  end

  if hit.Parent:FindFirstChild("Humanoid") then
    debounce = true
    for _, position in pairs(positions) do
      local part = Instance.new("Part")
      part.Parent = workspace
      part.Position = tile.Position + position
    end
   debounce = false
  end
end

tile.Touched:Connect(handleTouched)
2 Likes
local tile = script.Parent
local debounce = false
local values = {10, -10, 10, -10}

tile.Touched:Connect(function(hit)
	if debounce then
		return
	end
	debounce = true
	if hit.Parent:FindFirstChild("Humanoid") then
		debounce = true
		for i = 1, 4 do
			local part = Instance.new("Part")
			part.Parent = workspace
			if i <= 2 then
				part.Position = tile.Position + Vector3.new(values[i],0,0)
			elseif i >= 3 then
				part.Position = tile.Position + Vector3.new(0,0,values[i])
			end
		end
	end
	task.wait(3)
	debounce = false
end)

I’ve also made it so that the cooldown now works.

2 Likes

Well done for spotting the missing cool down, but unless there is some magic in indexing an array I missed this going to error as there are only two values in the array while the loop indexes 1 through 4

Thanks for letting me know, I was stuck between an array of 4 items or just subtracting 2 when indexing the array if i >= 3, I’ve decided to go with the former.

local tile = script.Parent
local debounce = false

tile.Touched:Connect(function(hit)
	if not debounce and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		debounce = true
		local part, s, p
		for i = 0, 3 do
			s = (-1)^i
			p = math.floor(i/2)%2
			part = Instance.new("Part")
			part.Parent = workspace
			part.Position = tile.Position + 10*Vector3.new(s*(1-p),0,s*p)
		end
	end
end)
2 Likes

Thanks! i have one question about the code though

in the code
for _, position in pairs(positions) do

what does the _, mean?

oh cool thanks! a little confusing for me, but it seems interesting.

It is a common convention to indicate the index variable populated by the pairs function is not used inside the loop.