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

My Output Box is Skipping the Set Increment from my For Loop, How do I fix?

I'm making a part gradually turn invisible when you click it, and then once fully invisible you can click it again and it turns visible again. You're supposed to be able to click it again and again, and it will repeatedly turn invisible/ visible over and over as long as you click it.

Here's my script:

transparencyPart = game.Workspace.transparencyPart


transparencyPart.ClickDetector.MouseClick:Connect(function()
	
if transparencyPart.Transparency == 0 then
	
	for i = 0, 1, .1 do
	
	transparencyPart.Transparency = i
	
	
	print(i)
	wait(.1)
	
	end
	
	
elseif transparencyPart.Transparency == 1 then
	
	for i = 1, 0, -.1 do
		
		transparencyPart.Transparency = i
		print(i)
		wait(.1)
		
	end	
	
	
end
	
end)

Now, this script works the first two clicks, it turns invisible, then visible again. The problem is in the output. After the second click, it turns visible again as wanted, and the output box counts down normally by 0.1 as the increment, UNTIL it counts down to 0.1 transparency. Then it suddenly jumps to 1.3877787807814e-16. The weird thing is the part stays completely visible at 0 transparency as if unaffected by the large number.

Heres my output box:

0 -- first click occurs
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1 -- end of first click
1 -- second click occurs
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1   -- next should be 0, instead it's this crazy number
1.3877787807814e-16

How can I fix this?

1 answer

Hello Consecrate_d! You need to add something simple in your script:

transparencyPart = game.Workspace.transparencyPart

transparencyPart.ClickDetector.MouseClick:Connect(function()
if transparencyPart.Transparency == 0 then
    for i = 0, 1, .1 do
    transparencyPart.Transparency = i
    print(i)
    wait(.1)
    end
else
	if transparencyPart.Transparency == 1 then
    for i = 1, 0.1, -.1 do
        transparencyPart.Transparency = i
        print(i)
        wait(.1)
        transparencyPart.Transparency = 0 -- It will change the value after the part appears to 0
      end 
	  end
end
end)

Check line: 16. I hope that helps you. Good luck.

1 comment

0

Thanks! That does help.

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