Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this if statement have any effect?

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

4 comments

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

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?

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?

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.

1 answer

✓ Accepted answer

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

5 comments

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

0

Are you sure? i tested it in studio and also checked that it toggles when it hits 0.5

0

I've accepted this answer cause you've basically solved my problem, but I'm not sure how to fix this last bit.

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

0

Ah, sorry about that. I made a minor edit. It works perfectly. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.