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

How to make a Touch Sensor into a Clickable Button?

I've taken some code from a friend, and the way they have written it means that a player has to touch a block for it to change a signal.

It would be better for it to be touch because we could save a lot of space, and reduce the chances of an error.

Here's the code:


local TweenService = game:GetService("TweenService")

local RELOAD_ENABLED = true -- Reload this script. Sometimes it may not work.
local TIME_BEFORE_RELOAD = 30

local disable = false
local reloadd = true

local st = script.Parent.Parent.SignalB
local stdeb = false
local sb = script.Parent.Parent.SignalT
local sbdeb = false

local stg, sty, str = script.Parent.SignalTopGreen, script.Parent.SignalTopYellow, script.Parent.SignalTopRed

local sbg, sby, sbr = script.Parent.SignalBottomGreen, script.Parent.SignalBottomYellow, script.Parent.SignalBottomRed

local re = script.Parent.Reload

local function Tween(instance, properties)
	TweenService:Create(
		instance,
		TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out),
		properties
	):Play()
end

spawn(function()
	re.BrickColor = BrickColor.Black()
	wait(TIME_BEFORE_RELOAD)
	re.BrickColor = BrickColor.new("Bright violet")
	reloadd = false
end)

stg.Touched:Connect(function()
	if disable then return end
	if stdeb then return else stdeb = true end
	Tween(st.LightGreen1, {Color = Color3.fromRGB(0, 255, 0)})
	Tween(st.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightRed1, {Color = Color3.fromRGB(27, 42, 53)})
	wait(0.5)
	stdeb = false
end)

sty.Touched:Connect(function()
	if disable then return end
	if stdeb then return else stdeb = true end
	Tween(st.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightYellow1, {Color = Color3.fromRGB(255, 170, 0)})
	Tween(st.LightRed1, {Color = Color3.fromRGB(27, 42, 53)})
	wait(0.5)
	stdeb = false
end)

str.Touched:Connect(function()
	if disable then return end
	if stdeb then return else stdeb = true end
	Tween(st.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightRed1, {Color = Color3.fromRGB(255, 0, 0)})
	wait(0.5)
	stdeb = false
end)

sbg.Touched:Connect(function()
	if disable then return end
	if sbdeb then return else sbdeb = true end
	Tween(sb.LightGreen1, {Color = Color3.fromRGB(0, 255, 0)})
	Tween(sb.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightRed1, {Color = Color3.fromRGB(27, 42, 53)})
	wait(0.5)
	sbdeb = false
end)

sby.Touched:Connect(function()
	if disable then return end
	if sbdeb then return else sbdeb = true end
	Tween(sb.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightYellow1, {Color = Color3.fromRGB(255, 170, 0)})
	Tween(sb.LightRed1, {Color = Color3.fromRGB(27, 42, 53)})
	wait(0.5)
	sbdeb = false
end)

sbr.Touched:Connect(function()
	if disable then return end
	if sbdeb then return else sbdeb = true end
	Tween(sb.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightRed1, {Color = Color3.fromRGB(255, 0, 0)})
	wait(0.5)
	sbdeb = false
end)

re.Touched:Connect(function()
	if not RELOAD_ENABLED then return end
	if reloadd then return end
	reloadd = true
	disable = true
	re.BrickColor = BrickColor.Black()
	wait(0.75)
	stdeb = false
	sbdeb = false
	Tween(sb.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(sb.LightRed1, {Color = Color3.fromRGB(255, 0, 0)})
	Tween(st.LightGreen1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightYellow1, {Color = Color3.fromRGB(27, 42, 53)})
	Tween(st.LightRed1, {Color = Color3.fromRGB(255, 0, 0)})
	wait(0.75)
	disable = false
	wait(TIME_BEFORE_RELOAD)
	re.BrickColor = BrickColor.new("Bright violet")
	reloadd = false
end)

1 answer

Try ClickDetector if you haven't already. Insert one into the parts you want to be clickable, then do something like

part1.ClickDetector.MouseClick:Connect(function()
	--code
end)

part2.ClickDetector.MouseClick:Connect(function()
	--code
end)

--and so on

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