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

Why does this script only work once?

Asked by 9 years ago

This script is supposed to turn the blocks transparency from 1, to 0.9. It does, but only once, my other script has when the block is clicked, it turns the blocks transparency back to 1, But the 1 to 0.9 script only works once, why is that so?

here is the 1 to 0.9 script

if
    script.Parent.Transparency == 1 then
    wait(5)
    script.Parent.Transparency = 0.9
end

and here is the click script

function click()
    script.Parent.Transparency = 1
end

script.Parent.ClickDetector.MouseClick:connect(click)

Thank you

3 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

It only works once because if statements only run once.

If statements only care about the condition they check at that specific time. An analogy would be if you fail to turn in your homework. The teacher doesn't care if you turn in your homework next week, you're still going to get a bad grade for the homework you failed to turn in the previous week. If statements work the same way. They check the condition once, and only once, and make their decision based on the data they gather at that specific time.

You could put the entire thing in an eternal loop, so it keeps running the if statement.

while true do
    wait(0.5)
    --code
end

You could use a changed event, which will fire every time a property changes.

script.Parent.Changed:connect(function()
    --code
end)

It depends what you're trying to do.

Ad
Log in to vote
1
Answered by
Scubadoo2 115
9 years ago

The problem is that the if statement will only check when the game starts. it doesn't activate whenever the Transparency changes, just when the script runs. If you want it the check whenever the transparency changes, then you need to hook it up to the Changed event, like this:

script.Parent.Changed:connect(function(property)

    if  script.Parent.Transparency == 1 then
      wait(5)
       script.Parent.Transparency = 0.9
    end

end)

Now it will check the transparency whenever the brick changes, Transparency or not.

if you want it more efficient, then you might want to combine the two scripts so that it doesn't have to constantly listen to the Changed effect, but I am assuming there is a reason why they are two separate scripts.

Log in to vote
-1
Answered by 9 years ago

You should just loop it.

Answer this question