Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you break a loop remotely using a localscript?

So, basically, I wanted to make a menu GUI.

I used this script to insert a localscript into my player:

game.Players.PlayerAdded:connect(function(player)
    script.LocalScript:clone().Parent = player.CharacterAdded:wait()
end)

and then this is the localscript:

local target = workspace.Part
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
local angle = 0
 
while wait() do
    camera.CoordinateFrame = CFrame.new(target.Position)
                           * CFrame.Angles(0, angle, 0)
                           * CFrame.new(0, 10, 5)
    angle = angle + math.rad(1)
end

Basically, this makes it so that the camera circles around the part in Workspace.

My idea is that once you press play, the camera returns to the player and you can walk around.

Here's the script inside the play button:

script.Parent.MouseButton1Click:connect(function(plr)
game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"
end)

The problem is that once I "fix" the camera back to the player, it circles around the player because the loop is never broken.

I want it so that it stops circling around the player and the player can move their camera around freely, but I don't know how to break the loop remotely.

Please help, thanks.

1 comment

0

Consider just disabling the camera script with the button script. That would do it. Or, stick a BoolValue somewhere accessible by both scripts: while cameraRotate.Value == true and wait() do

1 answer

Basically what TheDeadlyPanther said but turned into an answer

You could delete the script inside the player: game.Players.LocalPlayer.Character.LocalScript:Destroy()

Or you could make a boolvalue which constantly gets checked by the camera script

local target = workspace.Part
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
local angle = 0

while script.Rotate.Value == true and wait() do
    camera.CoordinateFrame = CFrame.new(target.Position)
                           * CFrame.Angles(0, angle, 0)
                           * CFrame.new(0, 10, 5)
    angle = angle + math.rad(1)
end

Button click

script.Parent.MouseButton1Click:connect(function(plr)
game.Players.LocalPlayer.Character.LocalScript.Rotate.Value = false
game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.