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

How to always check if value is a specific variable until it reaches that variable?

I'm trying to make a script keep checking a part's transparency frequently.. The part's default transparency is zero, and another script is coded to change the transparency to 1. The main script is supposed to constantly check what the transparency is, and once it finds out the transparency is 1, it'll stop checking. How do I do this? (Just so you know, the transparency checker script is a local script)

I try writing this:

if workspace.Part.Transparency == 1 then
	--enter action here
end

It obviously doesn't work because the script runs when the player spawns, and it only checks once.

1 answer

✓ Accepted answer

You can spawn a function that uses a while loop to check. In my opinion it's better to spawn a function instead of using a while loop because you can keep using the rest of the script even while the loop is running.

Then use a .Changed event to check to see each time if the transparency has changed.

local x = 1  -- So you can break the function whenever you want, just set x to another value.  
spawn(function()
	workspace.Smoke.Changed:Connect(function()
		while x == 1 do
			wait()
			if workspace.Smoke.Transparency == 1 then
				workspace.Smoke.Transparency = 0
			else
				return
			end
		end
	end)
end)

2 comments

0

??? This is silly. This creates a new loop every time smoke changes!

0

yeah i thought about it was I being super extra, it'd be better to just do a .Changed event and leave it at that.

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