This is my short amount of coding
function ot() script.Parent.BrickColor = BrickColor.new("Bright red") script.Parent.Transparency = .5 wait(0.25) script.Parent.BrickColor = BrickColor.new("Institutional white") script.Parent.Transparency = .5 end script.Parent.Touched:connect(ot)
I figured out how to make it so that when touched, these bricks change colors. Not too hard. The problem comes when I decide that I don't want to just have as a one time function, but rather I would like it to constantly be the 2nd color while there is something touching the brick.
I found my solution in "while", but I don't know how to implement it into this appropriately. Please help.
This is very similar to this question. You can read more there for more perspective on this problem.
while
uses a condition -- something that is either true
or false
-- to decide to stop or keep going. Touched
doesn't give you anything like that -- an event is just a point in time, but it doesn't give you a way to ask "is this part still touching"
There's a corresponding TouchEnded
event, though, that you can use.
local brick = script.Parent -- When you find yourself repeating yourself, something's amiss -- Use variables to shorten things! function startTouch() brick.BrickColor = BrickColor.new("Bright red") end function endTouch() brick.BrickColor = BrickColor.new("Institutional white") end brick.Touched:connect(startTouch)
This is a very simple first solution to this problem. As with the other question, there are problems with it: * if any part, not just the original touching part, stops touching the button, it goes back to white
We can fix that using the same techniques from that other question
I have never used GetTouchingParts
-- I'm hoping it works as expected.
Since we the color to always represent whether or not it's being touched, we need something that is always working. We can make a while true do
loop to make a loop that will never stop working.
local brick = script.Parent while true do wait() -- Breathing room -- otherwise it's too eager and will kill ROBLOX local partsTouching = brick:GetTouchingParts() end
We act based on whether or not there are any parts touching it. partsTouching
is a list -- there is nothing touching it if that list is empty. We can check if it's empty by seeing if its length is equal to 0:
local brick = script.Parent while true do wait() -- Breathing room -- otherwise it's too eager and will kill ROBLOX if #partsTouching == 0 then brick.BrickColor = BrickColor.new("Institutional white") else brick.BrickColor = BrickColor.new("Bright red") end end