Stop door from overshooting position?

local Workspace = game:GetService("Workspace")
local TweenService = game:GetService("TweenService")

local door = Workspace.Door
local button = script.Parent
local clickDetector = button.ClickDetector

local isDoorActive = false
local isCoolingDown = false

local function mouseClick(playerWhoClicked: Player)
	if isCoolingDown then
		return
	end
	isDoorActive = not isDoorActive
	
	if isDoorActive then
		local endCFrame = door.CFrame * CFrame.new(0, -7.7, 0)
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(20, 125, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	elseif not isDoorActive then
		local endCFrame = door.CFrame * CFrame.new(0, 7.7, 0)
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(125, 20, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	end
end

clickDetector.MouseClick:Connect(mouseClick)

Im making a FNAf style door where it goes up and down when u click a button, but the problem is, if u click it too fast then it overshoots the position that it is supposed to go to, I know I can fix this by making the cooldown longer but I want to keep the same cooldown but prevent it from going past where its supposed to go

it’s caused by the fact you’re using an offset and updating it, (the CFrame * CFrame.new), could store it outside of the function (so it stays the same) but a better option to do would be to set the beginning and end value as absolute (so clicking too fast doesn’t affect it)

Could you elaborate (30----------)

right here each time the function fires it gets the CURRENT CFrame (so if it’s in the middle of it’s movement it will still get offset by your * CFrame.new(0, -7.7, 0))

local function mouseClick(playerWhoClicked: Player)
	if isCoolingDown then
		return
	end
	isDoorActive = not isDoorActive
	
	if isDoorActive then
		local endCFrame = door.CFrame * CFrame.new(0, -7.7, 0) --This here
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(20, 125, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	elseif not isDoorActive then
		local endCFrame = door.CFrame * CFrame.new(0, 7.7, 0) -- This here
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(125, 20, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	end
end

clickDetector.MouseClick:Connect(mouseClick)

an easy fix is storing the open and close cframe (so it isn’t variable)

Thanks

local originCFrame = door.CFrame

local function mouseClick(playerWhoClicked: Player)
	if isCoolingDown then
		return
	end
	isDoorActive = not isDoorActive
	
	if isDoorActive then
		local endCFrame = originCFrame * CFrame.new(0, -7.7, 0)
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(20, 125, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	elseif not isDoorActive then
		local endCFrame = originCFrame
		TweenService:Create(door, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out), {CFrame = endCFrame}):Play()
		button.Color = Color3.fromRGB(125, 20, 20)
		isCoolingDown = true
		task.delay(.75, function()
			isCoolingDown = false
		end)
	end
end

clickDetector.MouseClick:Connect(mouseClick)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.