✓ Accepted answer
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
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
Using GetTouchingParts
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()
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()
if #partsTouching == 0 then
brick.BrickColor = BrickColor.new("Institutional white")
else
brick.BrickColor = BrickColor.new("Bright red")
end
end
2 comments
You mean you want the part to stay Institutional white as long as a player is making contact with that part?
Other way around. I'd like it to stay Bright Red while the player is making contact with it. :P I suppose I probably should of provided a little more information. Institutional White is the original Part color.