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