While true loop wont end when it detects mouse click again

In this script I am trying to end the loop again once the mousebutton1 is clicked, other then that, this script isnt completely organized yet, but it is supposed to move around a part to the mouses position

This is the script right now, and I’m at a lose on how I can fix this, any help is appreciated

	while true do
			UIS.InputBegan:Connect(function(key, processed)
				if processed then
					return
				end
				if key.UserInputType.EnumType == Enum.UserInputType.MouseButton2 then
			-- Would break loop, but it never did work (had a variable here etc.)
				end
			end)
			local mouse = game.Players.LocalPlayer:GetMouse()
			local MousePosOut = game.Players.LocalPlayer:GetMouse().hit.p

			Outline1x1.PrimaryPart:PivotTo(CFrame.new(MousePosOut.X,MousePosOut.Y  + Outline1x1.Union.ExtentsSize.Y / 2, MousePosOut.Z)) 
			mouse.TargetFilter = Outline1x1
			Outline1x1.Parent = workspace
			task.wait(0.004)
			
		end

thanks again

3 Likes
local mouse = game.Players.LocalPlayer:GetMouse()
local stop = true
UIS.InputBegan:Connect(function(key, processed)
				if processed then
					return
				end
				if key.UserInputType.EnumType == Enum.UserInputType.MouseButton2 then
			Stop = false
				end
			end)
while stop == true do
			local MousePosOut = game.Players.LocalPlayer:GetMouse().hit.p

			Outline1x1.PrimaryPart:PivotTo(CFrame.new(MousePosOut.X,MousePosOut.Y  + Outline1x1.Union.ExtentsSize.Y / 2, MousePosOut.Z)) 
			mouse.TargetFilter = Outline1x1
			Outline1x1.Parent = workspace
			task.wait(0.004)
			
		end

Never put a anything with “connect” in a while loop like that where you repeat with disconnections because that can cause massive memory leaks and performance issues.

When using a while loop you can put conditions after the why or you could use a repeat

repeat
-- code
until -- Condition is met
4 Likes

stop = false

this may be the reason why

1 Like

Right now, you’re looping over the InputBegan connection. Every time it loops, you connect the event. This means that you’re going to have tons and tons of events connected to InputBegan. Always connect events on their own.

local looping = true

UIS.InputBegan:Connect(Function(key)
if key.UserInputType == Enum.UserInputType.MouseButton1 then
looping = false
end
end)

while looping do
-- ...
end
1 Like

Okay then…

local runLoop = true

UIS.InputBegan:Connect(function(key, processed)
    if processed then
        return
    end
    if key.UserInputType == Enum.UserInputType.MouseButton2 then
        runLoop = false
    end
end)

while runLoop do
    local mouse = game.Players.LocalPlayer:GetMouse()
    local MousePosOut = mouse.hit.p

    Outline1x1.PrimaryPart:PivotTo(CFrame.new(MousePosOut.X, MousePosOut.Y + Outline1x1.Union.ExtentsSize.Y / 2, MousePosOut.Z)) 
    mouse.TargetFilter = Outline1x1
    Outline1x1.Parent = workspace
    task.wait(0.004)
end

-- Additional cleanup or code after the loop ends
print("Loop ended")
2 Likes

your lower loop may be going so fast that’s all you’re going to get out of it
can’t you .Stepped:Wait() … probably on the verge of locking up

local rs = game:GetService(“RunService”)
rs.Stepped:Wait()

maybe you can’t but that still seems a bit fast … might just be overwhelming the mouse read

1 Like

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