Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use "while"?

Asked by 9 years ago

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.

0
You mean you want the part to stay Institutional white as long as a player is making contact with that part? alphawolvess 1784 — 9y
0
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. AecTalek 5 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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


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() -- 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
Ad

Answer this question