How do I cancel something inside of a Tween?

So I am working on an inventory/interaction script for a little game I’m working on, however I have ran into an interesting problem. My Problem is that the item the player is collecting is still getting collected even though the player lifted their mouse left click.

I have made a tween inside of a Button1Down function, and I’ve been trying all kinds of ways as to how I can get around doing this.

In a gif what is supposed to happen is Player lets go of their mouse, which makes the interaction gui go invisible. What is supposed to happen then is the interaction overall is supposed to stop, however my code is still reaching the “PickupItem:FireServer(item)” line. I need a way to essentially cancel doing that FireServer if the Player lifts his finger from holding left click.

Here is my local script:

local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickupItem = ReplicatedStorage:WaitForChild("PickupItem")
local pickupKey = "F"

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local PlayerGui = player:WaitForChild("PlayerGui")
local interaction = PlayerGui:WaitForChild("MainGui").Interaction

mouse.Move:Connect(function()
	interaction.Position = UDim2.new(0, mouse.X + 15, 0, mouse.Y - 35)
end)

UIS.InputChanged:Connect(function(input)
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.Target
			interaction.Visible = true
			interaction.ObjectName.Text = item.Name
		else
			interaction.Visible = false
		end
	end
	print(input)
end)

mouse.Button1Down:Connect(function()
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.Target
			if item then
				local distanceFromItem = player:DistanceFromCharacter(item.Position)
				if distanceFromItem < 30 then
					interaction.Bar.Juice:TweenSize(UDim2.new(0, 251, 0, 10), "Out", "Linear", 1, true, function()
						mouse.Button1Up:Connect(function()
							interaction.Visible = false
							return
						end)
						interaction.Bar.Juice:TweenSize(UDim2.new(0, 0, 0, 10), "Out", "Linear", 0, true)
					end)
					wait(1)
					PickupItem:FireServer(item)
				end
			end
		end
	end
	
end)

What you should do is have a variable in your script called “finished”
and set it to false in the part where you close the GUI.

Then you only pick it up if the variable is true.

3 Likes