So, I'm learning how to script and I'm messing around with some scripts just for fun. I made one to turn the transparency of the baseplate 0.5 and to turn it 0 and now I need a button to turn it on and off. I want to click the button to make the baseplate transparent and opaque. Here is the script:
local function Transparent(part) --function to turn transparent part.Transparency = 0.5 end local function NotTransparent(part) --function to turn opaque part.Transparency = 0 end local naotransparente = NotTransparent(mybaseplate) --variable to apply the opacity to the baseplate local transparente = Transparent(mybaseplate) --variable to apply the transparency to the baseplate local mybaseplate(Game.Workspace.BasePlate) --variable to simplify the baseplate identity
So what script can I write to make a button? To click it and turn something on and off?
Okay, to start this off, here's the wiki link to ClickDetector.
http://wiki.roblox.com/index.php?title=API:Class/ClickDetector
I will be explaining the given script.
Firstly, we will need a Part
. This part is the one that is going to be clicked.
local part = Instance.new("Part", workspace)
Secondly, we want to create a ClickDetector
instance. A ClickDetector
is used to detect when a part (or model) is clicked on by a player. This allows the player to create buttons, without the need for tools. The MouseClick
event must be handled by a Script. The Clickdetector
must be parented to the part or model which will be clicked. In this case, it will be the part we previously made.
local part = Instance.new("Part", workspace) -- Create a part local clickDetector = Instance.new("ClickDetector", part) -- Put the clickdetector in the part.
Thirdly, We want to make a function of what happens when the clickdetector
is fired (it is clicked) . It is in this function that we have the basic script which you want to run.
local function onMouseClick(player) -- Make the function part.BrickColor = BrickColor.random() -- Change the brickcolor to something random. end
Lastly, we want to connect our function to the ClickDetector
. So, like said before, whenever it is clicked, it will fire the function.
clickDetector.MouseClick:connect(onMouseClick)
So in all, the script will be this:
local part = Instance.new("Part", workspace) -- Create the Part local clickDetector = Instance.new("ClickDetector", part) -- Create the ClickDetector local function onMouseClick(player) -- Make the function part.BrickColor = BrickColor.random() -- Change the brickColor end clickDetector.MouseClick:connect(onMouseClick) -- Connect the function to the clickDetector.