How to end this script with a TextButton?

I want to make it where, when you press a GUI TextButton this script ends.
Any ideas?

game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraPart.CFrame
until game.Workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable and game.Workspace.CurrentCamera.CFrame == game.Workspace.CameraPart.CFrame

while wait() do
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraPart.CFrame
end
2 Likes

Have a variable, clicked, set to true when clicked, and have the loop run while clicked.

Also I removed some bad practices

local camera = game:GetService("Workspace").CurrentCamera
local clicked = false
local RunService = game:GetService("RunService")

camera:GetPropertyChanged("CameraType"):Connect(function()
    camera.CameraType = Enum.CameraType.Scriptable -- Don't use a loop! That is inefficient.
end)

button.Activated:Connect(function()
    clicked = true
end)

while not clicked do
    camera.CFrame = part.CFrame
    RunService.RenderStepped:Wait()
end

RenderStepped should be used for camera stuff. In this case, before every render frame, the camera is being moved to the part, while the button is not clicked.

5 Likes

This code is very vague and it looks like something got cut off at the top. Anyways if you want this to “end” and have the players camera set back to normal, just do this above your while loop.

local cam = workspace.CurrentCamera
local TextButton = "Put your text button here"

TextButton.Activated:Connect(function()
	cam.CameraType = "Custom"
	script:Destroy()
end)
2 Likes

Alright thanks, just one question this following script does not seem to work why so?

local TextButton = game.StarterGui.TeamSelect.Frame.Advertisers
repeat wait()
	game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraPart.CFrame
until game.Workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable and game.Workspace.CurrentCamera.CFrame == game.Workspace.CameraPart.CFrame

while wait() do
	game.Workspace.CurrentCamera.CFrame = game.Workspace.CameraPart.CFrame
end

TextButton.Activated:Connect(function()
	game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	script:Destroy()
end)
1 Like

The local script I gave you should be under the text button. At the top of the script you can do local button = script.Parent to get the button relative to the script. Don’t use the code you had. It has bunch of anti-patterns.

1 Like

This is the script I used (Note: This is a LocalScript in the StarterPlayerScripts.)

local camera = game:GetService("Workspace").CurrentCamera
local clicked = false
local RunService = game:GetService("RunService")
local button = game.StarterGui.TeamSelect.Frame.Advertisers
camera:GetPropertyChanged("CameraType"):Connect(function()
    camera.CameraType = Enum.CameraType.Scriptable 
end)

button.Activated:Connect(function()
    clicked = true
end)

while not clicked do
    camera.CFrame = game.Workspace.CameraPart.CFrame
    RunService.RenderStepped:Wait()
end
1 Like

That is your issue. You’re getting it from StarterGui.

When a player (re) spawns, StarterGui’s contents are cloned into the local player’s PlayerGui. This is false for ScreenGuis with the ResetOnSpawn property disabled.

The buttons they click, the other UI they see, the local/module scripts that run, are running inside PlayerGui.

If you move this local script inside the text button, you can do local button = script.Parent to easily get the button.

1 Like

Here is the script that is inside of the CameraPart, would this be needed?

local XPos = script.Parent.Orientation.X

local ZPos = script.Parent.Orientation.Z

local Index = 0

local Speed = 0.4

while wait() do

Index = Index + 1

script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(0,math.rad(Speed),0)

script.Parent.Orientation = Vector3.new(XPos, script.Parent.Orientation.Y, ZPos)

end
1 Like

No… are you misunderstanding something because I gave you the fix.

1 Like

Yes, I did what you said. Is this following script required elsewise?

No I don’t believe so, I do recommend following the links I put in my main reply. The ones about wait().

Hmmm odd, according to what you said, you have to put the LocalScript as the Children of the TextButton, but what makes the actual function spin? In a simpler way of saying - It is inoperative.

1 Like

Is it depending on something that makes it not work? Was it the code you provided?

1 Like

I got rid of the code I had already, this is just the LocalScript you gave me.

1 Like