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

Why won't this slider script work?

So I have script that uses CFrame to basically 'slide' the door open. I've used my old template which opened the door, waited a second or two then closed by itself. This script however, I remade from scratch in hopes of making it so that once its open it stays open and must be clicked again in order to be closed. This is the script. Interestingly enough, no errors pop up but the door fails to open whenever I click it.

bin = script.Parent
check1 = script.Parent.Open --A boolvalue used to confirm if the door is open or not.
check2 = script.Parent.Close -- Another boolvalue used to confirm if the door is closed or not
debounce = true
check1 = false
check2 = true

function open()
	if debounce == true then
		debounce = false
	end
	if check1 == false then
		check1 = true
	end
	if check2 == true then
		check2 = false
	end
	for i = 1,90 do
		bin.CFrame = CFrame.new(bin.CFrame.x,bin.CFrame.y,bin.CFrame.z - .1)
		wait()
	end
	debounce = true
end

function close()
	if debounce == true then
		debounce = false
	end
	if check1 == true then
		check1 = false
	end
	if check2 == false then
		check2 = true
	end
	for i = 1,90 do
		bin.CFrame = CFrame.new(bin.CFrame.x,bin.CFrame.y,bin.CFrame.z + .1)
		wait()
	end
	debounce = true
end

bin.ClickDetector.MouseClick:connect(open)
bin.ClickDetector.MouseClick:connect(close)

1 answer

✓ Accepted answer

What's happening is that you've connected the MouseClick event to both opening and closing the door at the same time. Due to how you've scripted it, the functions end up cancelling each other out.

You need to connect the MouseClick event to a single function that decides whether to open or close the door based on a variable (ex, "doorIsOpen")

I recommend moving your debounce to this new function. Your current debounce doesn't do anything - you need to return from the function if its the wrong value. The new function would look like this:

debounce = false
function OpenOrClose()
	if debounce then return end
	debounce = true
	if doorIsOpen then
		close()
		doorIsOpen = false
	else
		open()
		doorIsOpen = true
	end
	debounce = false
end

An alternative solution is to just add the variable "doorIsOpen" and then return immediately from the open() function if doorIsOpen, and similarly return immediately from the close() function if not doorIsOpen. Set doorIsOpen to the proper value at the end of the open/close functions.

1 comment

0

I did this earlier it turns out an 'elseif' statement is needed instead of the 'else' at line 8. Other than that thanks!

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