Camera Bug for Maze Generator Game

I am making a Maze Generator Game, I have a bug right now (actually muiltple). The camera glitches out. Below is an image representing the bug. I am not sure actually which script it that causes the bug but below the image is the only script the manipulates the camera.

Here is the script:

-- Variables
local GenerateMaze = require(script:WaitForChild("MazeGenerationModule"))

local generatingFrame = script.Parent:WaitForChild("GeneratingFrame")
local levelFrame = script.Parent:WaitForChild("LevelFrame")
local title = script.Parent:WaitForChild("Title")
local easyButton = levelFrame:WaitForChild("EasyButton")
local normalButton = levelFrame:WaitForChild("NormalButton")
local hardButton = levelFrame:WaitForChild("HardButton")
local insaneButton = levelFrame:WaitForChild("InsaneButton")

local cam = workspace.CurrentCamera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local inMenu = true
local debounce = false

local maxTilt = 10

-- Menu Camera
repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until
cam.CameraType == Enum.CameraType.Scriptable

task.spawn(function()
	repeat
		cam.CFrame = CFrame.new(0, 10, 0) * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
		task.wait()
	until inMenu == false
	
	cam.CameraType = Enum.CameraType.Custom
end)

-- Frame tween
local function LevelFrameTweenOut()
	inMenu = false
	levelFrame:TweenPosition(UDim2.fromScale(1.3, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, -0.6), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	task.wait(0.7)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(1,1), UDim2.fromScale(0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
end

local function LevelFrameTweenIn()
	inMenu = true
	levelFrame:TweenPosition(UDim2.fromScale(0.325, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, 0.088), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	
	game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(0, 5, 0)
	
	repeat
		wait()
		cam.CameraType = Enum.CameraType.Scriptable
	until
	cam.CameraType == Enum.CameraType.Scriptable

	task.spawn(function()
		repeat
			cam.CFrame = CFrame.new(0, 10, 0) * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
			task.wait()
		until inMenu == false

		cam.CameraType = Enum.CameraType.Custom
	end)
end

-- Easy
easyButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(0)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Normal
normalButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(1)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Hard
hardButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(2)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Insane
insaneButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(3)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
	LevelFrameTweenIn()
	GenerateMaze.UnLoad()
end)

This code it for a menu for selecting the maze type.
Thank you for your help in advance! :slight_smile:

Here is the link for the game so you can see what is happening.
Maze Generation [v.1.0.0]

repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until

you’re creating an inf loop here, which is repeatly setting your camera to a custom one

2 Likes

Thank you for your help but how will I make it so this will not happen.

Btw awesome character.

should just be able to remove that code

1 Like

That did not work however the code was useless anyways.

i played the game and it seemed to be working, also you have another inf loop in your levelFrameTweenIn function

1 Like

Did you reset, die or reset the maze and try again. That is where the error is.

on the screen Gui there is a property that stops the Gui from resetting after you die, that may be useful in this case

1 Like

I do not understand why that fixed it but it did!

1 Like

The problem came back! Please help again! :slight_smile: :pray:

what is your problem? β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž β€Ž

So the camera does not go back to custom despite all. The new script is below and for some reason it breaks after generating a second maze.

-- Variables
local RunService = game:GetService("RunService")
local GenerateMaze = require(script:WaitForChild("MazeGenerationModule"))

local generatingFrame = script.Parent:WaitForChild("GeneratingFrame")
local levelFrame = script.Parent:WaitForChild("LevelFrame")
local title = script.Parent:WaitForChild("Title")
local easyButton = levelFrame:WaitForChild("EasyButton")
local normalButton = levelFrame:WaitForChild("NormalButton")
local hardButton = levelFrame:WaitForChild("HardButton")
local insaneButton = levelFrame:WaitForChild("InsaneButton")

local cam = workspace.CurrentCamera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local inMenu = true
local debounce = false

local maxTilt = 10

-- Menu Camera
repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until cam.CameraType == Enum.CameraType.Scriptable

local function UpdateCam()
	cam.CFrame = CFrame.new(0, 10, 0) * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
	task.wait()
end

local connection = RunService.RenderStepped:Connect(UpdateCam)

-- Frame tween
local function LevelFrameTweenOut()
	levelFrame:TweenPosition(UDim2.fromScale(1.3, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, -0.6), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	task.wait(0.7)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(1,1), UDim2.fromScale(0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	
	connection:Disconnect()
	connection = nil
	
	repeat
		task.wait()
		cam.CameraType = Enum.CameraType.Custom
	until cam.CameraType == Enum.CameraType.Custom
end

local function LevelFrameTweenIn()
	levelFrame:TweenPosition(UDim2.fromScale(0.325, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, 0.088), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	
	game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(0, 5, 0)
	
	repeat
		wait()
		cam.CameraType = Enum.CameraType.Scriptable
	until cam.CameraType == Enum.CameraType.Scriptable

	connection = RunService.RenderStepped:Connect(UpdateCam)
end

-- Easy
easyButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(0)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Normal
normalButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(1)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Hard
hardButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(2)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

-- Insane
insaneButton.MouseButton1Click:Connect(function()
	LevelFrameTweenOut()
	GenerateMaze.Generate(3)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	task.spawn(function()
		repeat
			task.wait()
		until #game.Workspace.Maze:GetChildren() == 0

		LevelFrameTweenIn()
	end)
	
	-- Exit
	game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
			debounce = true
			local newParticle = script.ParticleEmitter:Clone()
			newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
			script.Sound:Play()
			task.wait(3)
			newParticle:Destroy()
			GenerateMaze.UnLoad()
			game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
			debounce = false
		end
	end)
end)

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
	LevelFrameTweenIn()
	GenerateMaze.UnLoad()
end)

Thank you for the help!

see if that helps you with your camera

-- Variables
local RunService = game:GetService("RunService")
local GenerateMaze = require(script:WaitForChild("MazeGenerationModule"))

local generatingFrame = script.Parent:WaitForChild("GeneratingFrame")
local levelFrame = script.Parent:WaitForChild("LevelFrame")
local title = script.Parent:WaitForChild("Title")
local easyButton = levelFrame:WaitForChild("EasyButton")
local normalButton = levelFrame:WaitForChild("NormalButton")
local hardButton = levelFrame:WaitForChild("HardButton")
local insaneButton = levelFrame:WaitForChild("InsaneButton")

local cam = workspace.CurrentCamera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local inMenu = true
local debounce = false

local maxTilt = 10

-- Menu Camera
repeat
	wait()
	cam.CameraType = Enum.CameraType.Scriptable
until cam.CameraType == Enum.CameraType.Scriptable

local function UpdateCam()
	cam.CFrame = CFrame.new(0, 10, 0) * CFrame.Angles(math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),0)
end

local connection

local function ConnectCamera()
    if connection then connection:Disconnect() end
    connection = RunService.RenderStepped:Connect(UpdateCam)
end

local function DisconnectCamera()
    if connection then
        connection:Disconnect()
        connection = nil
    end
end

ConnectCamera()

-- Frame tween
local function LevelFrameTweenOut()
	levelFrame:TweenPosition(UDim2.fromScale(1.3, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, -0.6), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	task.wait(0.7)
	generatingFrame:TweenSizeAndPosition(UDim2.fromScale(1,1), UDim2.fromScale(0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	
	DisconnectCamera()
	
	cam.CameraType = Enum.CameraType.Custom
end

local function LevelFrameTweenIn()
	levelFrame:TweenPosition(UDim2.fromScale(0.325, 0.223), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
	title:TweenPosition(UDim2.fromScale(0.375, 0.088), Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.25)
	
	game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(0, 5, 0)
	
	cam.CameraType = Enum.CameraType.Scriptable
	ConnectCamera()
end

local function SetupButton(button, difficulty)
    button.MouseButton1Click:Connect(function()
        LevelFrameTweenOut()
        GenerateMaze.Generate(difficulty)
        generatingFrame:TweenSizeAndPosition(UDim2.fromScale(0, 0), UDim2.fromScale(0.5, 0.5), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.7)
        task.spawn(function()
            repeat
                task.wait()
            until #game.Workspace.Maze:GetChildren() == 0
            LevelFrameTweenIn()
        end)

        -- Exit
        game.Workspace.Maze:FindFirstChild("Exit").Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
                debounce = true
                local newParticle = script.ParticleEmitter:Clone()
                newParticle.Parent = game.Workspace.Maze:FindFirstChild("Exit")
                script.Sound:Play()
                task.wait(3)
                newParticle:Destroy()
                GenerateMaze.UnLoad()
                game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("AddWin"):FireServer(game.Players:GetPlayerFromCharacter(hit.Parent))
                debounce = false
            end
        end)
    end)
end

-- Setup all buttons
SetupButton(easyButton, 0)
SetupButton(normalButton, 1)
SetupButton(hardButton, 2)
SetupButton(insaneButton, 3)

game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
	LevelFrameTweenIn()
	GenerateMaze.UnLoad()
end)