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

Why doesn't this if statement have any effect?

Asked by
Hypgnosis 186
5 years ago
Edited 5 years ago

Lines 3 through 7 have no effect on the part. No errors show in output. The rest works exactly as I want, but I need help with the if statement.

local cube = script.Parent

while true do

    if cube.Transparency < 0.5 then
        cube.CanCollide = false
    else
        cube.CanCollide = true
    end

   for i = 0,1,0.01 do
      cube.Transparency = i
      wait(0.05)
   end

    for i = 1,0,-0.01 do
        cube.Transparency = i
        wait(0.05)
    end

    wait(0.01)  
end
0
Once the game starts, the script will check the cube transparency. Then it will run the while loop which changes the transparency. You would need to include the if statement into you're while loop to make it do what you want to do. :D Creeperman1524 120 — 5y
0
I thought the same, but it still has no effect. The part remains collide-able whether the if statement is in or out of the loop. Any other ideas? Hypgnosis 186 — 5y
0
I don't see why putting it in the loop doesn't work. Could you update your code so I can see what you had done? Creeperman1524 120 — 5y
0
There. Also, I've discovered something. When I set the the sign to 'less than, (<)' the part has no collide throughout the test. If I set the sign to 'greater than, (>)' the part always has collide throughout the test. Hypgnosis 186 — 5y

1 answer

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago
Edited 5 years ago

It is because scripts run from the top down, you have to include the condition checking in your for loops

The If statement only gets checked when the while loop repeats, and when the loop repeats the Cube transparency is 0 which is why CanCollide is always false if its < and true if its > in your script. Thats why your if statement must be in the for loop not the while loop

local cube = script.Parent

while true do



   for i = 0,1,0.01 do

   if cube.Transparency > 0.5 then
        cube.CanCollide = true
    end
      cube.Transparency = i
      wait(0.05)
   end

    for i = 1,0,-0.01 do
        if cube.Transparency < 0.5 then
        cube.CanCollide = false
        end
        cube.Transparency = i
        wait(0.05)
    end

    wait(0.01)  
end
0
Your explanation makes a lot of sense, and your code is a step in the right direction, but the CanCollide condition only changes at Transparency 1 and 0, instead of Transparency 0.5 Hypgnosis 186 — 5y
0
Are you sure? i tested it in studio and also checked that it toggles when it hits 0.5 aazkao 787 — 5y
0
I've accepted this answer cause you've basically solved my problem, but I'm not sure how to fix this last bit. Hypgnosis 186 — 5y
0
your code is exactly the same as mine? i can guarantee that my code will toggle the CanCollide at 0.5 transparency,tested in studio myself aazkao 787 — 5y
0
Ah, sorry about that. I made a minor edit. It works perfectly. Thanks! Hypgnosis 186 — 5y
Ad

Answer this question