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

Why isn't my spectate GUI working?

Added it to a localscript so I could use player = game.Players.LocalPlayer but now it wont work, the event won't fire :(

2 questions: 1.) How can I fix this script 2.) If I had this in a server script, how would I find the local player (eg. script.Parent.Parent.Parent.Parent, where is player/character located?)

gui = script.Parent players = game.Workspace.plyrs:GetChildren() print(script.Parent.Parent.Parent.Parent.Parent.Parent.Name) player = game.Players.LocalPlayer spectating = false

gui.MouseButton1Click:connect(function(mouse)
	players = game.Workspace.plyrs:GetChildren()
	if spectating == false and #players > 0 and not game.Workspace:FindFirstChild(player.Name) then
		print("Attempting to spectate")
		spectating = true
		magic = math.random(1,#players)
		Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
		mouse.KeyDown:connect(function(key)
			players = game.Workspace.plyrs:GetChildren()
			if key == "f" then
				Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid	
			end
			if spectating == true and #players > 0 and not game.Workspace:FindFirstChild(player.Name) then
				if key == "e" then
					if magic ~= #players then
						magic = magic + 1
					else
						magic = 1
					end		
					Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
				end
				if key == "q" then
					if magic ~= 1 then
						magic = magic - 1
					else
						magic = #players
					end		
					Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
				end
			else 
				Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid	
				spectating = false
			end
		end)			
	else
		Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid
		spectating = false	
	end	
end)

1 answer

I can only answer the first question as i do not quite understand your second. (Hey, I make games where you don't need to spectate) But I do see a problem. Neither #players or spectating is defined before the if statement which causes an error for an unknown name. To fix, try this.

spectating = false -- shows that they are not spectating
#players = -- I don't know what to put here. I already told you i don't need spectate in my games.
 
gui.MouseButton1Click:connect(function(mouse)
    players = game.Workspace.plyrs:GetChildren()
    if spectating == false and #players > 0 and not game.Workspace:FindFirstChild(player.Name) then
        print("Attempting to spectate")
        spectating = true
        magic = math.random(1,#players)
        Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
        mouse.KeyDown:connect(function(key)
            players = game.Workspace.plyrs:GetChildren()
            if key == "f" then
                Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid   
            end
            if spectating == true and #players > 0 and not game.Workspace:FindFirstChild(player.Name) then
                if key == "e" then
                    if magic ~= #players then
                        magic = magic + 1
                    else
                        magic = 1
                    end     
                    Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
                end
                if key == "q" then
                    if magic ~= 1 then
                        magic = magic - 1
                    else
                        magic = #players
                    end     
                    Workspace.CurrentCamera.CameraSubject = game.Players:FindFirstChild(players[magic].Name).Character.Humanoid
                end
            else 
                Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid   
                spectating = false
            end
        end)            
    else
        Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid
        spectating = false  
    end 
end)

1 comment

0

The # in #players returns the number of (entries?) in a table. He did define the table in line 5. I do agree with you that spectating needs to be defined, though.

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