Hello! So I’m having this problem, I have a button that tweens a part. But when I click the button for the first time it doesn’t work, but it works on 2nd click. This is my script
But when I move the part that suppose to move before I click the button, it just teleports to the target.
— [[VARIABLES]] —
local TweenService = game:GetService(“TweenService”)
local ScriptParent = script.Parent
local Detector = ScriptParent.Detector
local Button = ScriptParent.Button
local MovingProp = script.Parent.Parent.Parent
local PropSetting = MovingProp.PropSetting
local PropTargets = MovingProp.PropTargets
local MovingPart = MovingProp.MovingPart
local PropMoveSpeed = PropSetting.PropMoveSpeed
local PropMoveStatus = PropSetting.PropMoveStatus
— [[FUNCTIONS]] —
Cooldown = false
Detector.MouseClick:Connect(function()
local TweenInformation = TweenInfo.new(PropMoveSpeed.Value, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
if not Cooldown then
Cooldown = true
ScriptParent.Button.Color = Color3.fromRGB(250, 250, 0)
Detector.MaxActivationDistance = 0
---
if PropMoveStatus == false then
local MoveProp = TweenService:Create(MovingPart, TweenInformation, {CFrame = PropTargets.Target2.CFrame}) MoveProp:Play()
PropMoveStatus = true
else
local MoveProp = TweenService:Create(MovingPart, TweenInformation, {CFrame = PropTargets.Target1.CFrame}) MoveProp:Play()
PropMoveStatus = false
end
wait(PropMoveSpeed.Value)
---
Cooldown = false
ScriptParent.Button.Color = Color3.fromRGB(0, 250, 0)
Detector.MaxActivationDistance = 20
end
How @9_Spy said you might change from “PropMoveStatus = true” to "PropMoveStatus.Value = true " because PropMoveStatus is an object(BoolValue) not a boolean(true or false). “if PropMoveStatus == false then” is like this “if object == false” so it will go to the “else”. That makes that the movingPart tweens to the it same position.
Try this:
local TweenService = game:GetService("TweenService")
local ScriptParent = script.Parent
local Detector = ScriptParent.Detector
local Button = ScriptParent.Button
local MovingProp = script.Parent.Parent.Parent
local PropSetting = MovingProp.PropSetting
local PropTargets = MovingProp.PropTargets
local MovingPart = MovingProp.MovingPart
local PropMoveSpeed = PropSetting.PropMoveSpeed
local PropMoveStatus = PropSetting.PropMoveStatus
local Cooldown = false
Detector.MouseClick:Connect(function()
if Cooldown then return end
Cooldown = true
local TweenInformation = TweenInfo.new(PropMoveSpeed.Value, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
ScriptParent.Button.Color = Color3.fromRGB(250, 250, 0)
Detector.MaxActivationDistance = 0
local MoveProp = {CFrame = PropTargets.Target2.CFrame}
if PropMoveStatus.Value then
MoveProp = {CFrame = PropTargets.Target1.CFrame}
end
PropMoveStatus.Value = not PropMoveStatus.Value
MoveProp = TweenService:Create(MovingPart, TweenInformation, MoveProp)
MoveProp:Play()
wait(PropMoveSpeed.Value)
MoveProp:Destroy()
Cooldown = false
ScriptParent.Button.Color = Color3.fromRGB(0, 250, 0)
Detector.MaxActivationDistance = 20
end)