Breaking a loop when mousebutton1 is pressed

I have a script to pick up an object, but when I want to let go by using MouseButton1, the loop wont break. If there is a way to fix this problem I would gladly appreciate it. Heres the script

local inputservice = game:GetService('UserInputService')
local tweenService = game:GetService('TweenService')

local player = game.Players.LocalPlayer
local part = workspace.Part
local camera = workspace.CurrentCamera

local info = TweenInfo.new(
	.1,
	Enum.EasingStyle.Linear
)

local debounce = false



inputservice.InputBegan:Connect(function(input, IS)
	if IS == true then return end
	if input.KeyCode == Enum.KeyCode.F and not debounce then -- if pressed then the part will start tween
		part.CanCollide = false
			while true do
				wait()
				
				if input.KeyCode == Enum.UserInputType.MouseButton1 then --this is what i want to use to break the loop
					print('broken')
					break
				end
				
				local targetCFrame = camera.CFrame * CFrame.new(0,-2,-5) -- parts position to be in front of the camera
				local tween = tweenService:Create(part, info, {CFrame = targetCFrame}) -- tween that makes the part move smoothly to the camera
				tween:Play()
			end
	end
end)


Since this is a localscript, how about using the player’s mouse instead of userinputservice to detect the clicking?

1 Like

How would I be able to do that?

here is the updated code.

local inputservice = game:GetService('UserInputService')
local tweenService = game:GetService('TweenService')

local player = game.Players.LocalPlayer
local part = workspace.Part
local camera = workspace.CurrentCamera
local mouse=player:GetMouse()--gets the player's mouse
local info = TweenInfo.new(
	.1,
	Enum.EasingStyle.Linear
)

local debounce = false



inputservice.InputBegan:Connect(function(input, IS)
	if IS == true then return end
	if input.KeyCode == Enum.KeyCode.F and not debounce then -- if pressed then the part will start tween
		part.CanCollide = false
			while true do
				wait()
				
				mouse.MouseButton1Down:Connect(function() --this is what i want to use to break the loop
					print('broken')
					break
				end)
				
				local targetCFrame = camera.CFrame * CFrame.new(0,-2,-5) -- parts position to be in front of the camera
				local tween = tweenService:Create(part, info, {CFrame = targetCFrame}) -- tween that makes the part move smoothly to the camera
				tween:Play()
			end
	end
end)

you get the mouse using player:GetMouse(), then you connect the MouseButton1Down event to break the loop

1 Like

It doesn’t work. The problem with using a function in a loop is that you cannot break the loop if the ‘break’ is inside the function. I also tried another thing with the code you gave me. I tried an


inputservice.InputBegan:Connect(function(input, IS)
	if IS == true then return end
	if input.KeyCode == Enum.KeyCode.F and not debounce then -- if pressed then the part will start tween
		part.CanCollide = false
		while true do
			wait()
			if mouse.Button1Down then --this is the change i added
				print('broken')
				break
			end

			local targetCFrame = camera.CFrame * CFrame.new(0,-2,-5) -- parts position to be in front of the camera
			local tween = tweenService:Create(part, info, {CFrame = targetCFrame}) -- tween that makes the part move smoothly to the camera
			tween:Play()
		end
	end
end)

But even after using that, it only worked once the function that its in is started, but not when I wanted it to work when button1 is down. Its weird.

What you can do is move the mouse.Button1Down:Connect(function() outside the function, then add a boolean and when the mouse clicks, set it to true, and replace if mouse.Button1Down then with: if boolean==true then and add boolean=false in it with break

1 Like

Oh my gosh thank you for helping me it worked!!

1 Like

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