✓ Accepted answer
Hi. There are a couple reasons why this is not creating the desired effect. The first problem is the placement of your debounce on line 19. The reason for this is because on line 15 you wait 5 seconds.If someone were to click the detector in those 5 seconds, the function would think that debounce was still false. This is what is causing the speedup. Secondly, you are connecting heartbeat more than once. Heartbeat will continue to execute every frame until you disconnect it, which you never do. This means that every time someone clicked the detector a new heartbeat connection is made. This is really bad for performance, and after some clicking your game's server will start to lag. You can check the script performance tab to see what I mean. As you keep clicking the Activity % of the script will go up.
A better way of doing this that would have the same effect is using the TweenService. What this does is smoothly transition different properties over time.
local TweenService = game:GetService("TweenService");
local partA = workspace.A
local partB = workspace.B
local partC = workspace.C
local Button = workspace.Button
local speed = 2
local lock = false;
local TweenDetails = TweenInfo.new(
5,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
0,
false,
0
);
local function Unlock()
lock = false;
end
local function Tween2()
local TweenProperties = {
["CFrame"] = partB.CFrame
};
local Tween = TweenService:Create(partC, TweenDetails, TweenProperties);
Tween:Play();
Tween.Completed:Connect(Unlock);
end
Button.ClickDetector.MouseClick:Connect(function()
if lock == false then
lock = true;
local TweenProperties = {
["CFrame"] = partA.CFrame
};
local Tween = TweenService:Create(partC, TweenDetails, TweenProperties);
Tween:Play();
Tween.Completed:Connect(Tween2);
end
end)
However if you are insistent on making the original script work, I have fixed it here.
local RunService = game:GetService("RunService")
local partA = workspace.A
local partB = workspace.B
local partC = workspace.C
local Button = workspace.Button
local speed = 2
local lock = false;
local HeartbeatFunction;
local function CtoA(deltaTime)
local deltaSpeed = deltaTime * speed
local CFrameLerpA = partC.CFrame:Lerp(partA.CFrame, deltaSpeed)
partC.CFrame = CFrameLerpA
end
local function CtoB(deltaTime)
local deltaSpeed = deltaTime * speed;
local CFrameLerpB = partC.CFrame:Lerp(partB.CFrame, deltaSpeed)
partC.CFrame = CFrameLerpB
end
Button.ClickDetector.MouseClick:Connect(function()
if lock == false then
lock = true;
HeartbeatFunction = RunService.Heartbeat:Connect(CtoA);
wait(5);
HeartbeatFunction:Disconnect();
HeartbeatFunction = RunService.Heartbeat:Connect(CtoB);
wait(5);
HeartbeatFunction:Disconnect();
lock = false;
end
end)
Let me know if these don't work or you have further questions.
2 comments
Thank you so much! Both of them worked perfectly!
No problem. Glad I could help!