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

Spectate script makes camera freeze in place?

I followed a tutorial for how to make a spectating script from a video on Youtube made one year ago(video maker is TheIceZombieSlayer Videos). Everything works fine, except every so often when I enter my game the camera gets stuck in one place and I cannot move it, while the rest of my game runs like normal(resetting does not help). It doesn't seem like anyone in the comment section of the video had the same problem I am having. I will paste my code below:

Player = game.Players.LocalPlayer
ScGUI = script.Parent
Camera = game.workspace.CurrentCamera
UIS = game:GetService("UserInputService")

Spectating = false
Index = 1

Camera.CameraSubject = Player.Character

function Watch(player)
	if player and player.Character and Camera.CameraSubject ~= player.Character then
		Camera.CameraSubject = player.Character
		ScGUI.SpectateFrame.TextLabel.Text = "*"..player.Name.."*"
	end
end

function ChangeState(bool)
	if bool == nil then
		Spectating = not Spectating
	else
		Spectating = bool
	end
	
	if Spectating then
		ScGUI.SpectateButton.Image = "rbxassetid://3376963769"
		ScGUI.SpectateFrame.Visible = true
	else
		Watch(Player)
		ScGUI.SpectateButton.Image = "rbxassetid://3364442186"
		ScGUI.SpectateFrame.Visible = false
	end
end


function CurrentCourse()
	return workspace.CurrentCourse:GetChildren()[1]
end

function IncrementIndex(num)
	local TempIndex = Index + num
	local Course = CurrentCourse()
	local TempContestants = Course.CourseInfo.TempContestants
	local Contestants = TempContestants:GetChildren()
	
	if TempIndex > #Contestants then
		TempIndex = 1
	elseif TempIndex < 1 then
		TempIndex = #Contestants
	end
	Index = TempIndex
end

ScGUI.SpectateButton.MouseButton1Click:connect(function ()
	ChangeState()
end)


UIS.InputBegan:connect(function (Input)
	if Input.KeyCode == Enum.KeyCode.Q then
		IncrementIndex(-1)
	elseif Input.KeyCode == Enum.KeyCode.E then
		IncrementIndex(1)
	end
end)

ScGUI.SpectateFrame.PreviousButton.MouseButton1Click:connect(function ()
	IncrementIndex(-1)
end)
ScGUI.SpectateFrame.NextButton.MouseButton1Click:connect(function ()
	IncrementIndex(1)
end)


game:GetService("RunService").RenderStepped:connect(function ()
	local Course = CurrentCourse()
	if Course then
		local TempContestants = Course:WaitForChild("CourseInfo"):WaitForChild("TempContestants")
		local Contestants = TempContestants:GetChildren()
		
		if not TempContestants:FindFirstChild(Player.Name) then
			ScGUI.SpectateButton.Visible = true
			if Spectating then
				IncrementIndex(0)
				Watch(Contestants[Index].Value)
			end
		else
			ScGUI.SpectateButton.Visible = false
			ChangeState(false)
		end
	else
		ScGUI.SpectateButton.Visible = false
		ChangeState(false)
	end
end)

Can someone please explain how I can fix this glitch?

2 comments

0

Does the console show any errors?

0

@Sonnenroboter no, the camera freezes with no errors

1 answer

✓ Accepted answer

There you go bud

repeat wait() until game.Players.LocalPlayer.Character -- In order to make sure that the character fully added.
Player = game.Players.LocalPlayer
ScGUI = script.Parent
Camera = game.workspace.CurrentCamera
UIS = game:GetService("UserInputService")

Spectating = false
Index = 1

Camera.CameraSubject = Player.Character

function Watch(player)
    if player and player.Character and Camera.CameraSubject ~= player.Character then
        Camera.CameraSubject = player.Character
        ScGUI.SpectateFrame.TextLabel.Text = "*"..player.Name.."*"
		Camera.CameraType="Custom" -- To make the camera moveable
    end
end

function ChangeState(bool)
    if bool == nil then
        Spectating = not Spectating
    else
        Spectating = bool
    end

    if Spectating then
        ScGUI.SpectateButton.Image = "rbxassetid://3376963769"
        ScGUI.SpectateFrame.Visible = true
    else
        Watch(Player)
        ScGUI.SpectateButton.Image = "rbxassetid://3364442186"
        ScGUI.SpectateFrame.Visible = false
    end
end


function CurrentCourse()
    return workspace.CurrentCourse:GetChildren()[1]
end

function IncrementIndex(num)
    local TempIndex = Index + num
    local Course = CurrentCourse()
    local TempContestants = Course.CourseInfo.TempContestants
    local Contestants = TempContestants:GetChildren()

    if TempIndex > #Contestants then
        TempIndex = 1
    elseif TempIndex < 1 then
        TempIndex = #Contestants
    end
    Index = TempIndex
end

ScGUI.SpectateButton.MouseButton1Click:connect(function ()
    ChangeState()
end)


UIS.InputBegan:connect(function (Input)
    if Input.KeyCode == Enum.KeyCode.Q then
        IncrementIndex(-1)
    elseif Input.KeyCode == Enum.KeyCode.E then
        IncrementIndex(1)
    end
end)

ScGUI.SpectateFrame.PreviousButton.MouseButton1Click:connect(function ()
    IncrementIndex(-1)
end)
ScGUI.SpectateFrame.NextButton.MouseButton1Click:connect(function ()
    IncrementIndex(1)
end)


game:GetService("RunService").RenderStepped:connect(function ()
    local Course = CurrentCourse()
    if Course then
        local TempContestants = Course:WaitForChild("CourseInfo"):WaitForChild("TempContestants")
        local Contestants = TempContestants:GetChildren()

        if not TempContestants:FindFirstChild(Player.Name) then
            ScGUI.SpectateButton.Visible = true
            if Spectating then
                IncrementIndex(0)
                Watch(Contestants[Index].Value)
            end
        else
            ScGUI.SpectateButton.Visible = false
            ChangeState(false)
        end
    else
        ScGUI.SpectateButton.Visible = false
        ChangeState(false)
    end
end)

1 comment

0

Thanks a million, EDLLT!

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