How would i make it so that all parts can be touched and fire the 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 = workspace.Triangle
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)
local debounce2 = true
function SlowPlayer(part)
local hum = part.Parent:FindFirstChild(“Humanoid”)
if hum and debounce2 then
debounce2 = false
local OriginalWalkSpeed = hum.WalkSpeed
hum.WalkSpeed = hum.WalkSpeed - 10
wait(5)
hum.WalkSpeed = OriginalWalkSpeed
end
end
workspace.Triangle.Touched:Connect(SlowPlayer)

i want to make it so that any part thats called Triangle gets touched, it fires the command above. What this does right now, is just choses the first part and only that will fire when touched
Im pretty sure you have to change the code to server script service but please let me know.

also two things:

1: A lot of you forumers told me some things to help shorten my script. It was really helpful but kind of difficult for me to manage especially if I wanted to change what the property’s of the clones were

2: If you have spare time, I know there’s probably a way to make the script look more clean and shorter (because at the moment its very messy)

Thanks everyone for being awesome. Im going to go to sleep but i look forward to replies in the morning :smiley:

Do you want to make touchavle everything in the workspace or in a folder?

For i, v in pairs(object/folder:GetChildren()) do
      v.Touched:Connect(function(hit)
             --put your script you want to fire in here
      end)
end
local tile = script.Parent
local debounce = false
local debounce2 = true
local axisValue = {10, -10, 10, -10}
local triangle = workspace:WaitForChild("Triangle")

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

while task.wait() do
	for _, triangle in ipairs(workspace:GetChildren()) do
		if triangle.Name == "Triangle" then
			triangle.Touched:Connect(function(hit)
				if debounce2 then
					return
				end
				local hum = hit.Parent:FindFirstChild("Humanoid")
				if hum then
					debounce2 = true
					local currSpeed = hum.WalkSpeed
					hum.WalkSpeed = hum.WalkSpeed - 10
					task.wait(5)
					hum.WalkSpeed = currSpeed
					debounce2 = false
				end
			end)
		end
	end
end
1 Like
local tile = script.Parent
local debounce = false
local debounce2 = true
local axisValue = {10, -10, 10, -10}
local triangle = workspace:WaitForChild("Triangle")

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

workspace.ChildAdded:Connect(function(child)
	if child.Name == "Triangle" then
		child.Touched:Connect(function(hit)
			if debounce2 then
				return
			end
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum then
				debounce2 = true
				local currSpeed = hum.WalkSpeed
				hum.WalkSpeed = hum.WalkSpeed - 10
				task.wait(5)
				hum.WalkSpeed = currSpeed
				debounce2 = false
			end
		end)
	end
end)

Two different ways of achieving this.

1 Like