Whats wrong with my script?

for some reason, when I was making a camera script for a menu. The camera cframe does not go back to the player. is this a roblox bug?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local PlayButton = script.Parent.TextButton
Camera.CameraType = Enum.CameraType.Scriptable
while game.Workspace.IsPlaying.Value == false do
	task.wait()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = workspace.MenuCamPart.CFrame
	print("testing repeat")
end
PlayButton.MouseButton1Click:Connect(function()
	Camera.CameraType = Enum.CameraType.Custom
        game.Workspace.IsPlaying.Value = true
end)
2 Likes

The problem is in this line:

This will cause an infinite loop. The loop continues, without reaching this part:

Therefore, it never becomes true, creating an infinite loop.

The simplest solution is to move the connection above the loop.

However, there is a big performance problem in your code and I’d also like to help you fix that.

The problem is that the loop is unnecessary. You keep setting the values, but they were already set to that value previously. You just need to set them once, no loop is needed.

Just remove the loop, that will fix that performance problem. You also won’t need game.Workspace.IsPlaying unless you use it outside of this script. I will keep it for now because you probably do use it elsewhere.

The code should look like this in the end:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local PlayButton = script.Parent.TextButton

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = workspace.MenuCamPart.CFrame

PlayButton.MouseButton1Click:Connect(function()
	Camera.CameraType = Enum.CameraType.Custom
        game.Workspace.IsPlaying.Value = true
end)

In this code the place of the connection doesn’t matter, because there is no loop this time.

2 Likes

ohh I see the confusion, well whats suppose to happen is that the camera moves around in the opening. but for some reason, when the button is pressed, the print stops. however the camera cframe does not go back to the character.

1 Like

also sorry to say but the script you put on the bottom, does not put the camera to its cframe.

I also tried using while true do. However the camera moves around, but does not go back to the character.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local PlayButton = script.Parent.TextButton
Camera.CameraType = Enum.CameraType.Scriptable
while game.Workspace.IsPlaying.Value == false do
	task.wait()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = workspace.MenuCamPart.CFrame
	print("testing repeat")
end
PlayButton.MouseButton1Click:Connect(function()
	game.Workspace.IsPlaying.Value = true
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Character.Humanoid
end)
1 Like

Oh, I assume the connection is in another script?

Anyway, now I (hope that I) actually see why your method doesn’t work. You first set the camera type to custom, no problem there. however, the problem is with the task.wait and the place in which you set that value in. The value is set to true, and the CameraType is Custom, however, you probably are in the middle of the wait, which means it will be set back to Scriptable.

well, whenever I put I remove the scriptable part in the loop. the script completely breaks and does not move the camera.

I honestly do think this might just be a roblox engine bug. ive been stuck on this for about a week so.

1 Like

If it is contact @Bug-Support, but I doubt it is so. I have a game with a title screen and I basically do this:

And it works almost flawlessly, even when it doesn’t, that’s unrelated to the camera thing. Have you seen any errors/interesting behavior with the any of the solutions?

By the way, I think you should try moving the wait from the beggining of the while loop to the end, if what I said in my previous reply was correct.

2 Likes

well the confusing part is, there are no errors. I’ve tried this method too.

free model - (eww!!! free models!!!)

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local PlayButton = script.Parent.PlayButton

repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
until Camera.CameraType == Enum.CameraType.Scriptable
Camera.CFrame = workspace.CameraPart.CFrame

PlayButton.MouseButton1Click:Connect(function()
	Camera.CameraType = Enum.CameraType.Custom
	PlayButton:Destroy()
end)

edited version -

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local PlayButton = script.Parent.TextButton
Camera.CameraType = Enum.CameraType.Scriptable
repeat wait()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = workspace.MenuCamPart.CFrame
until Camera.CameraType == Enum.CameraType.Custom

PlayButton.MouseButton1Click:Connect(function()
	Camera.CameraType = Enum.CameraType.Custom
end)
1 Like

Is that the case?

I’ve got no idea about how your code even works to some extent if that is the case. Also, you should try printing when MouseButton1Click fires.

1 Like

oh the connection is in the same script, when the player presses the button, it makes it so that the camera is suppose to go back to the player.

1 Like

Try this

PlayButton.MouseButton1Click:Connect(function()
game.CurrentCamera.CameraType = Enum.CameraType.Custom
game.Workspace.IsPlaying.Value = true
end)

1 Like

I found the solution! turns out that there was another script that makes the button disappear before the main script was able to run. thank you all for the help!

1 Like