Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

SpectateGui Attempt to index local 'player' (a nil value)?

Asked by 7 years ago
Edited 7 years ago
local players = nil
local number = 0
local maxnumber = 0
local player = game.Players.LocalPlayer
script.Parent.Parent:WaitForChild("PlayerInfo"):WaitForChild("pName")

script.Parent.Next.MouseButton1Click:connect(function(right)
    players = game.Players:GetChildren()
    maxnumber = #players+1
    number = (number + 1) % maxnumber
    if number == 0 then
        number = number+1
    end
    if players[number] ~= nil then
        script.Parent.Parent.PlayerInfo.Visible = true
        game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
        script.Parent.Parent.PlayerInfo:WaitForChild('pName').Text = players[number].Name
        coroutine.resume(coroutine.create(function()
            players[number].Character.Humanoid.Changed:connect(function()
                print(number)
                if players[number].Character.Humanoid.Health < 1 then
                    number = (number - 1) % maxnumber
                    if number == 0 then
                        number = number+1
                    end
                end
                if script.Parent.PlayerName.Text == players[number].Name then
                    script.Parent.Parent.PlayerInfo.Health.Text = math.floor(players[number].Character.Humanoid.Health)
                    if players[number].Character.Humanoid.Health > 100 then
                        script.Parent.Parent.PlayerInfo.Bar.BackgroundColor3 = Color3.new(0, 108/255, 191/255)
                        spawn(function()
                            if players[number].Character.Humanoid.Health < 120 then
                                script.Parent.Parent.PlayerInfo.Health.Text = math.floor(players[number].Character.Humanoid.Health)
                                script.Parent.Parent.PlayerInfo.Bar.BackgroundColor3 = Color3.new(0, 191/255, 0)
                            end
                        end)
                    end
                    script.Parent.Parent.PlayerInfo.Bar.Size = UDim2.new(players[number].Character.Humanoid.Health/players[number].Character.Humanoid.MaxHealth,0,.99,0)
                end
            end)
        end))
        if players[number].Name ~= player.Name then
            script.Parent.PlayerName.Text = players[number].Name
        else 
            script.Parent.Parent.PlayerInfo.Visible = false
            script.Parent.PlayerName.Text = "Nobody"
        end
    elseif players[number] == nil then
        number = 1
        game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
        if players[number].Name ~= player.Name then
            script.Parent.PlayerName.Text = players[number].Name
        else 
            script.Parent.Parent.PlayerInfo.Visible = false
            script.Parent.PlayerName.Text = "Nobody"
        end
    end
end)

script.Parent.Previous.MouseButton1Click:connect(function(left)
    players = game.Players:GetChildren()
    maxnumber = #players+1
    number = number - 1
    if number == 0 then
        number = maxnumber
    end
    if players[number] ~= nil then
        script.Parent.Parent.PlayerInfo.Visible = true
        game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
        script.Parent.Parent.PlayerInfo:WaitForChild('pName').Text = players[number].Name
        coroutine.resume(coroutine.create(function()

            players[number].Character.Humanoid.Changed:connect(function()
                if players[number].Character.Humanoid.Health < 1 then
                    number = (number + 1) % maxnumber
                    if number == 0 then
                        number = number+1
                    end
                end
                if script.Parent.PlayerName.Text == players[number].Name then
                    script.Parent.Parent.PlayerInfo.Health.Text = math.floor(players[number].Character.Humanoid.Health)
                    if players[number].Character.Humanoid.Health > 100 then
                        script.Parent.Parent.PlayerInfo.Bar.BackgroundColor3 = Color3.new(0, 108/255, 191/255)
                        spawn(function()
                            if players[number].Character.Humanoid.Health < 101 then
                                script.Parent.Parent.PlayerInfo.Health.Text = math.floor(players[number].Character.Humanoid.Health)
                                script.Parent.Parent.PlayerInfo.Bar.BackgroundColor3 = Color3.new(0, 191/255, 0)
                            end
                        end)
                    end
                    script.Parent.Parent.PlayerInfo.Bar.Size = UDim2.new(players[number].Character.Humanoid.Health/players[number].Character.Humanoid.MaxHealth,0,.99,0)
                end
            end)
        end))
        if players[number].Name ~= player.Name then
        script.Parent.Parent.PlayerInfo.Visible = true
            script.Parent.PlayerName.Text = players[number].Name
        else 
            script.Parent.Parent.PlayerInfo.Visible = false
            script.Parent.PlayerName.Text = "Nobody"
        end
    elseif players[number] == nil then
        number = maxnumber
        game.Workspace.CurrentCamera.CameraSubject = players[number].Character.Humanoid
        if players[number].Name ~= player.Name then
            script.Parent.PlayerName.Text = players[number].Name
        else 
            script.Parent.Parent.PlayerInfo.Visible = false
            script.Parent.PlayerName.Text = "Nobody"
        end
    end
end)

--[[script.Parent.Parent.SpectateButton.Spectate.MouseButton1Down:connect(function(click)
    if script.Parent.Parent.Active.Value == false then
        script.Parent.Parent.Active.Value = true
        script.Parent.Parent.ChangePlayer.Visible = true
    elseif script.Parent.Parent.Active.Value == true then
        script.Parent.Parent.Active.Value = false
        script.Parent.Parent.ChangePlayer.Visible = false
        script.Parent.PlayerName.Text = "Nobody"
        number = 1
        game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
    end
end)]]
0
what line is the error on? AstrealDev 728 — 7y
0
line 105 killzone45671 -20 — 7y
0
Is FE enabled? AstrealDev 728 — 7y
0
You check if player[num] equals nil, then you try to index it after the conditional OldPalHappy 1477 — 7y

Answer this question