Code issues - how i do part touched only 1 time

Hello, I would like to know how can i make the “part” execute the ‘if’ only if it has been touched 1 time and then wait 3 seconds to be able to touch it again and execute the ‘if’

how can i do that? :scream:

LocalScript:

-- items
local it1 = script.Parent.item1
local it2 = script.Parent.item2
local it3 = script.Parent.item3

-- Touches parts
local tpart1 = game.Workspace.Touching1 -- RED
local tpart2 = game.Workspace.Touching2 -- BLUE
local tpart3 = game.Workspace.Touching3 -- GREEN

-- tables
local obj = {"x","xx","xxx"}

-- touches
tpart1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it1)
		print("touches red part")
		wait(5)
		it1.BackgroundColor3 = tpart1.Color
		wait(5)
		print(obj)
	end
end)

tpart2.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it2)
		print("touches blue part")
		wait(5)
		it2.BackgroundColor3 = tpart2.Color
		wait(5)
		print(obj)
	end
end)

tpart3.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(obj,1,it3)
		print("touches green part")
		wait(5)
		it3.BackgroundColor3 = tpart3.Color
		wait(5)
		print(obj)
	end
end)

Images

Project organization

image

1 Like

Great! thanks you!

is it good?

-- Debounce system
local Debounce = false

-- tables
local obj = {"x","xx","xxx"}

-- touches
tpart1.Touched:Connect(function(hit)
	if not Debounce then -- dobounce
		if hit.Parent:FindFirstChild("Humanoid") then
			Debounce = true
			table.insert(obj,1,it1)
			print("touches red part")
			wait(5)
			it1.BackgroundColor3 = tpart1.Color
			wait(5)
			print(obj)
			Debounce = false
			wait(2)
		end
	end -- end debounce
end)

Looks fine, did you try it out to see if it works?

1 Like

It works perfect! Thank you very much, I had no idea how to do that xD :smiley:

1 Like