Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

.Touched() doesn't work on button that's anchored. How do I fix that?

I'm making a button that opens the gate for a certain amount of time. However when I touch the button nothing happens.

Here is the code I have:

repeat
	wait()
until game:IsLoaded()

local part = script.Parent
local gate = part.Parent
local gate_model = gate.Blocks

local time_number = gate.Time.Value

function button_press()
	for i, v in pairs(gate_model:GetChildren()) do
		v.Transparency = 1
		v.CanCollide = false
		
		wait(time_number)
		
		v.CanCollide = true
		if not v.Name == "Invisiwall" then
			v.Transparency = 0
		end
	end
end

part.Touched:Connect(function()
	print("TEST")
	button_press()
end)

Here is a video of what happens (humanoids are not anchored and you need one part to be unanchored for .Touched() to work. I can't unanchor it because the button has CanCollide off)

2 answers

There is a property called CanTouch that allows the .Touched event to be fired. This might be turned off.

If it is on, then it might also be because in that custom character maybe IT has that property off.

Really, this is a bit baffling, but this is my best guess.

2 comments

0

They both had CanTouch already on.

0

Im sorry, i really don't know.

Well, I didn't really understand what you wanted to do because my native language is not English and sometimes I can misinterpret what you asked, but if what you want to do is just make it so that when someone presses a button it opens a door for a certain amount of time and then closes it by itself I created it for you.

I created two templates, one I named door and the other as button, and inside the button I put a script.

Here are some variables that we will use

local button = script.Parent --Here we have our button 

local gate = script.Parent.Parent.Gate -- Here we have our Gate

local timeOpen = 5 --The time the gate remained open 

local buttonPressed = false --A variable that we will use so that it will not reset the gate 
							--if someone steps on the button again until it is closed again

Here we go if someone pressed the button

local function onTouch(touch) -- function that will be active when a player steps on the button 
	
	local character = touch.Parent -- let's get the one who touched the button 
	
	local humanoid = character:FindFirstChild("Humanoid") -- and here to see if the one who touched the button has a "Humanoid" 
														  --so there is no risk of another type of object touching it and activating it
	
	if humanoid and buttonPressed == false then -- here it checks if a "Humanoid" has touched the button and if the button has already been activated by someone
												
		openGate()	--If it is a "Humanoid" and the button has not been activated yet, it will execute our next function 
		
	end
	

	
	end


button.Touched:Connect(onTouch) -- This is to detect when something touches the button and activate the onTouch function we created 

So if someone pushed the button let's open our gate

local function openGate() --Here we have a function that will be activated when 
						  --someone activates the ringing function  
	
	buttonPressed = true 
	gate.Transparency = 1
	gate.CanCollide = false
	
	wait(timeOpen)				--as soon as our "openGate" function is activated the "buttonPressed" will become true and until the gate closes again no one can reset it 
	
	gate.Transparency = 0
	gate.CanCollide = true
	buttonPressed = false
	
end

Above I gave the script order of were contrary just to explain, in the script it looks like this

local button = script.Parent 

local gate = script.Parent.Parent.Gate 

local timeOpen = 5 

local buttonPressed = false 

local function openGate() 
	
	buttonPressed = true 
	gate.Transparency = 1
	gate.CanCollide = false
	
	wait(timeOpen)				
	
	gate.Transparency = 0
	gate.CanCollide = true
	buttonPressed = false
	
end

local function onTouch(touch) 
	
	local character = touch.Parent 
	
	local humanoid = character:FindFirstChild("Humanoid") 
														  
	
	if humanoid and buttonPressed == false then 
												
		openGate()	
		
	end
	

	
	end


button.Touched:Connect(onTouch) 

3 comments

0

Nope, still doesn't work.

0

Is it possible for you to enter here to solve a doubt of mine https://www.roblox.com/games/4756663288/Teste

This discussion is preserved as it was. New answers, comments, and votes are closed.