So I changed the math.max()
to math.clamp()
, so it moves gradually.
local part = script.Parent
local originCF
local clickDetector = part:WaitForChild("ClickDetector")
local targets = workspace:WaitForChild("Targets")
local target1 = targets:WaitForChild("1")
local RunService = game:GetService("RunService")
local cdCon cdCon = clickDetector.MouseClick:Connect(function(plr)
cdCon:Disconnect() -- Make sure player can't click again
local timePassed = 0
local totalTime = 5 -- seconds
local initialCF = part.CFrame
local con con = RunService.Heartbeat:Connect(function(dt)
timePassed += dt
local alpha = math.max(timePassed/totalTime, 0, 1)
print(alpha)
part.CFrame = initialCF:Lerp(target1.CFrame, alpha)
if timePassed >= totalTime then con:Disconnect() print(con.Connected) print(timePassed) end
end)
end)
Here’s the code.
I thought disconnecting a connection outside another connection will disconnect everything inside the connection, but no?