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

Hard to summarize in a one sentence summary [?]

What I'm trying to do is as following. When you click a button, it sets a state to false, and then when a loop has finished, it sets that state to true.

part = script.Parent    
startCFrame = part.CFrame 
openCFrame = CFrame.new(part.Parent.Open.Position) 
closeCFrame = CFrame.new(part.Parent.Close.Position) 
open = false
finished = true
part.ClickDetector.MouseClick:connect(function()
	if open == false and finished == true then	
	finished = false --set it to false
		for i = 0, 1, 0.01 do 
			part.CFrame = startCFrame:lerp(openCFrame,i);
			wait();
			open = true --When finished set it to true
			finished = true --When finished set it to true
		end
	elseif open == true and finished == true then	
		finished = false --Set it to false
		for i = 0, 1, 0.01 do 
			part.CFrame = startCFrame:lerp(closeCFrame,i);
			wait();
			open = false --When finished set it to true
			finished = true --When finished set it to true
		end
	end
end)

For those that don't understand

My question is quite simple.

How. Do. I. *Do. That.

3 comments

3

And... your question is?

0

I kinda stated my question... "What I'm trying to do is as following. When you click a button, it sets a state to false, and then when a loop has finished, it sets that state to true."

0

and what is it doing instead? (The "instead" part should be your question title)

1 answer

✓ Accepted answer

I don't know what your question is, but your code could be better.

Using == false and == true when you're comparing against booleans is redundant.

Just use if not open and finished then

Both of the condition check finished. Instead, try quitting early if not finished.

Note that you have if not open then .... elseif open then ..... You should just be using an else here.

Now look and see both branches begin with finished = false, so you can move that up to the top instead of being repeated twice.

Bugs

You set open and finished during the loop instead of after the loop. In particular, finished should just be set at the end of function and open immediately after the for loops.

startCFrame is only set once at the beginning of the script. You probably want to set it at the beginning of this event.

The end result looks like this:

part = script.Parent	
openCFrame = CFrame.new(part.Parent.Open.Position) 
closeCFrame = CFrame.new(part.Parent.Close.Position) 
open = false
finished = true
part.ClickDetector.MouseClick:connect(function()
	if not finished then
		return
	end
	local startCFrame = part.CFrame 
	finished = false --set it to false
	if not open then  
		for i = 0, 1, 0.01 do 
			part.CFrame = startCFrame:lerp(openCFrame,i);
			wait();
		end
		open = true --When finished set it to true
	else
		for i = 0, 1, 0.01 do 
			part.CFrame = startCFrame:lerp(closeCFrame,i);
			wait();
		end
		open = false --When finished set it to true
	end
	finished = true --When finished set it to true
end)

If you want, you can make this really short. open basically toggles which is the from CFrame and which is the to CFrame. So you could just do that directly:

part = script.Parent	
openCFrame = CFrame.new(part.Parent.Open.Position) 
closeCFrame = CFrame.new(part.Parent.Close.Position) 
finished = true
from, to = closeCFrame, openCFrame
part.ClickDetector.MouseClick:connect(function()
	if not finished then
		return
	end
	finished = false --set it to false
	for i = 0, 1, 0.01 do 
		part.CFrame = from:lerp(to,i);
		wait();
	end
	from, to = to, from -- flip from & to
	finished = true --When finished set it to true
end)

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