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

Problems with SetPrimaryPartCFrame, unsure of how to fix?

I'm trying to get my garage door to move up/down depending on it's status, so if the status is true, it moves up, and if it is false, it moves down.

movements=74
downmoves = -74
speed=0.1
status = false
script.Parent.Part.ClickDetector.MouseClick:connect(function()
	if status == false then
		for i=1,movements do
			wait(0.01)
			script.Parent.Door:SetPrimaryPartCFrame(script.Parent.Door.PrimaryPart.CFrame + Vector3.new(0,speed,0))
		end
		status = true
	elseif status == true then
		for i=1,downmoves do
			wait(0.01)
			script.Parent.Door:SetPrimaryPartCFrame(script.Parent.Door.PrimaryPart.CFrame + Vector3.new(0,-speed,0))
		end
		status = false
	end
end)

But, the door will go up no matter what. It will stop, but it can't come back down. How can I fix it?

1 answer

✓ Accepted answer

Your problem is on line 13. You're attempting to iterate from 1 to -74, which isn't possible when it counts up by 1. To fix this, change "downmoves" to 74 instead of -74!

Fixed code if you're confused (I also changed some small things a bit, shouldn't affect the overall script. I also added a debounce so that it doesn't break.):

local movements = 74
local speed = 0.1
local active = false
local status = false

script.Parent.Part.ClickDetector.MouseClick:connect(function()
	if not active then
		active = true
	    if not status then
	        for i=1,movements do
	            wait(0.01)
	            script.Parent.Door:SetPrimaryPartCFrame(script.Parent.Door.PrimaryPart.CFrame + Vector3.new(0,speed,0))
	        end
	        status = true
	    else
	        for i=1,movements do
	            wait(0.01)
	            script.Parent.Door:SetPrimaryPartCFrame(script.Parent.Door.PrimaryPart.CFrame + Vector3.new(0,-speed,0))
	        end
	        status = false
	    end
		active = false
	end
end)

If you need any more help, just ask!

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