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

Transparency Changing While Loop Needs To Break?

I'm making an obby, just for fun, and I have this script to change the transparency of these parts.

Here it is:

local translvl  = .1

function transpar(part)
	while true do
		if part.Transparency >= 1 then
			part.Transparency = part.Transparency - translvl
		elseif part.Transparency <= 0 then
		wait(3)
			part.Transparency = part.Transparency + 1
		end
	end
	wait(.1)
end

for _,parts in pairs(script.Parent:GetChildren()) do
	if parts.Name == "Part" then
		transpar(parts)
	end
end

So I'm trying to make it so the parts inside the model turn visible, don't do anything for 3 seconds and then slowly turn invisible again. Now I my game freezes and points to line 5. Any idea why this loop is running so many times?

Hope you can help and even if you can't thank you for your time.

3 comments

0

The Transparency of a BasePart cannot go over 1. Make >= 1 to == 1

0

I've found it can if you are doing a while loop of a variable and adding that to the transparency actually.

0

I think you meant to do 'if part.Transparency > 0 then', not 'if part.Transparency >= 1 then'

1 answer

✓ Accepted answer

Here's what you could do. I hope you find the comments helpful. This works I've already tested it.


--[[
    So I'm trying to make it so the parts inside the model turn visible, don't do anything for 3   seconds and then slowly turn invisible again.
--]]
local function childTransEffect(children, seconds)
	
    -- Iterate through the parents children and make them visible
    for i, v in ipairs(children) do
        -- Turn all children visible first
        if v.ClassName ==  "Part" or v.ClassName == "MeshPart" then
			if v.Name ~= "HumanoidRootPart" then
        		v.Transparency = 1
			end
        end
    end

    -- Wait the time
    wait(seconds)

	-- Then make transition to invisible
	for i = 1, 0, -0.1 do
		for k, v in ipairs(children) do
			if v.ClassName ==  "Part" or v.ClassName == "MeshPart" then
				if v.Name ~= "HumanoidRootPart" then
					v.Transparency = i -- assign the new value of transparency
				end
			end
		end
		wait(0.1)
	end
end

Then you could run it like this:

childTransEffect(script.Parent:GetChildren(), 3) -- The children reference, and the time to wait

3 comments

0

You’re using :IsA, and if the character has children that inherit from Part or MeshPart, get affected too. So do if v.ClassName == “Part”

0

Thanks for your feedback, that would work better!

0

Ok thanks, this works, and this is just a little step up from where I'm at right now with these for loops so it will be fun to disect and see what's going on even further and hopefully use it in later scripting!

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