Need help with loops

This is not an issue. Roblox handles these numbers for you.

local part = Instance.new('Part')
part.Transparency = 1
print(part.Transparency == 1) -- true

In the script he used, you can see he kept adding up decimal numbers. His issue was that the repeat until loop never finished because the condition was never met.

1 Like

If you are very confused on TweenService and want to stay with your original idea then use the following code

local boulder = script.Parent

while true do
	boulder.Transparency = boulder.Transparency + 0.1
	if boulder.Transparency >= 1 then
		boulder.Transparency = 0
	end
	wait(1)
end

2 Likes

So, it looks like your method works in one way. But part of it is still broken.

Here is the current code.

I added can collide more as a quality of life feature, and that works now that I used >= operator. But it still doesn’t go back to transparency 0.

Where do you make Transparency get set to 0?

1 Like

This only happens with tiny ass numbers, not 0.1. Computers can handle that math.

e.g.

local part = Instance.new('Part')
for i = 0, 1, (0.005) do 
	part.Transparency = i
end
print(part.Transparency) -- 0.9950000047683716 😬

and

local part = Instance.new('Part')
for i = 0, 1, (0.1) do 
	part.Transparency = i
end
print(part.Transparency) -- 1

My side shows otherwise…

image

Decimals on computers are wack.

1 Like

You are completely right, when I rewrote the code I must have forgot to add it back.

I believe you could just write

boulder.Transparency += 1

for simplicity

2 Likes

His goal is to make a fading transition (TweenService has already been suggested)

2 Likes

I see where you’re coming from now. That is a very serious issue that should not happen.

Thanks all for the help! Ory eventually helped me troubleshoot the end part. All good, and I learned quite a lot.

i was replying @darkvrn 's code, nothing to do with anyone else but them

Use TweenService instead.

local TweenService = game:GetService("TweenService")

local Boulder = script.Parent

local Info = TweenInfo.new(0.3,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut,0,false,0)

while true do -- Infinite loop (unless someone spills coffe on the server your game is running on)
	task.wait(0.3) -- Waits before restarting again
	local Tween = TweenService:Create(Boulder,Info,{Transparency = 1})
	Tween:Play() -- Plays the tween which smoothly modifies transparency
	Tween.Completed:Wait() -- Waits until the tween is finished
	Boulder.Transparency = 0 -- Resets the transparency
	task.wait(1) -- Waits before continuing loop again
end

Obviously you can tweak the wait times to give it the effect that you want. If this isn’t what you’re looking for, let me know.

1 Like

Oh lol, Im not very good with Luau maths

1 Like

Agreed, definitely learn TweenService. You won’t regret it!

The for loops are a hacky way to do this kind of thing which isn’t as smooth, and its harder to figure out how long you have to make the wait time in order for it to fit into a specified amount of seconds. TweenService does this all for you. Definitely learn to use it.

1 Like