Tween still moves when player stops holding mouse

local UserInputService = game:GetService("UserInputService")
local ticket = 0
local timeHeld = 0.2
local isHolding = false

local function StartRecoil()
	script.Parent:TweenPosition(UDim2.new(0.5, 0,0.45, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, .5, true)
	task.wait(.5)
	script.Parent:TweenPosition(UDim2.new(0.48, 0,0.4, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, .5, true)
	task.wait(.5)
	script.Parent:TweenPosition(UDim2.new(0.53, 0,0.4, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, .5, true)
	task.wait(.8)
	script.Parent:TweenPosition(UDim2.new(0.48, 0,0.38, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, .5, true)
	task.wait(.8)
	script.Parent:TweenPosition(UDim2.new(0.53, 0,0.4, 0), Enum.EasingDirection.In, Enum.EasingStyle.Sine, .5, true)
end

local function StopRecoil()
	script.Parent.Position = UDim2.new(.5, 0, .5, 0)
end

function leftMouseButtonHeld()
	isHolding = true
	StartRecoil()
end

function leftMouseButtonReleased()
	if not isHolding then return end
	isHolding = false
	StopRecoil()
end

UserInputService.InputBegan:connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		ticket = ticket + 1 -- Increase the ticket count, so that we can track if the user has actually held the mouse button or not
		local currentTicket = ticket -- Stores the CURRENT ticket value. If the user stops holding the mouse button, this will change and fail the if check

		if timeHeld > 0 then
			delay(timeHeld, function()
				if ticket == currentTicket then
					leftMouseButtonHeld()
				end
			end)
		else
			leftMouseButtonHeld()
		end
	end
end)

UserInputService.InputEnded:connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		ticket = ticket + 1		
		leftMouseButtonReleased()
	end
end)

Changing the position of the object doesn’t actually stop the tween. You’d need to make the tween that is currently in progress stop running, and you also need to make sure you don’t start any new tweens after you’ve already let go.

1 Like

local script 1

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local ticket = 0
local timeHeld = 0.2
local isHolding = false
local thread

function leftMouseButtonHeld()
	isHolding = true
	script.StartRecoil.Enabled = true
end

function leftMouseButtonReleased()
	if not isHolding then return end
	isHolding = false
	script.StopRecoil:Fire()
	script.StartRecoil.Enabled = false
end

UserInputService.InputBegan:connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		ticket = ticket + 1 -- Increase the ticket count, so that we can track if the user has actually held the mouse button or not
		local currentTicket = ticket -- Stores the CURRENT ticket value. If the user stops holding the mouse button, this will change and fail the if check

		if timeHeld > 0 then
			delay(timeHeld, function()
				if ticket == currentTicket then
					leftMouseButtonHeld()
				end
			end)
		else
			leftMouseButtonHeld()
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		ticket = ticket + 1		
		leftMouseButtonReleased()
	end
end)

local script 2


local pos1 = TweenService:Create(script.Parent.Parent, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {Position = UDim2.new(0.5, 0,0.45, 0)})
local pos2 = TweenService:Create(script.Parent.Parent, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {Position = UDim2.new(0.48, 0,0.4, 0)})
local pos3 = TweenService:Create(script.Parent.Parent, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {Position = UDim2.new(0.53, 0,0.4, 0)})
local pos4 = TweenService:Create(script.Parent.Parent, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {Position = UDim2.new(0.48, 0,0.38, 0)})
local pos5 = TweenService:Create(script.Parent.Parent, TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In), {Position = UDim2.new(0.53, 0,0.4, 0)})

local function StartRecoil()
	pos1:Play()
	task.wait(.5)
	pos2:Play()
	task.wait(.5)
	pos3:Play()
	task.wait(.8)
	pos4:Play()
	task.wait(.8)
	pos5:Play()
	task.wait(.5)
	script.Parent.Parent.Position = UDim2.new(.5, 0, .5, 0)
end

script.Parent.StopRecoil.Event:Connect(function()
	pos1:Cancel()
	pos2:Cancel()
	pos3:Cancel()
	pos4:Cancel()
	pos5:Cancel()
	script.Parent.Parent.Position = UDim2.new(.5, 0, .5, 0)
end)

spawn(function()
	StartRecoil()
end)

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