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
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
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
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")