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

Why won't my ServerScript go through the Player List?

I have a script that detects a change in a String Value called GameStatus. The change isn't the problem. The script checks the value of the string value for if it's a certain string and then runs a set of code indented.

The problem has been highlighted in comments.

local GameStatus = workspace.GameStatus
local HeadingCountdown = workspace.HeadingCountdown
local IntermissionTime = 30
local PlottingTime = 60
local PlotEvent = workspace.PlotEvent
local Players = game.Players
local TotalPlayers = Players:GetChildren()

GameStatus.Changed:Connect(function()
	
	if GameStatus.Value == "Intermission" then
		for i = IntermissionTime,0,-1 do --30 being the main value
			HeadingCountdown.Value = i
			print(i)
			wait(1)
		end
		GameStatus.Value = "Plotting"
		print(GameStatus,"is",GameStatus.Value,"on script")
		
		
		
	elseif GameStatus.Value == "Plotting" then
		print("WE GOT IT")
		PlotEvent:FireAllClients()
		for i = PlottingTime,0,-1 do
			HeadingCountdown.Value = i
			print(i)
			wait(1)
		end
		print("starting count")
		wait(3)
		----------------------------------------------------------------------PROBLEM
		for i,n in pairs(TotalPlayers) do
			print(TotalPlayers[n],"is up!")
			if n.Backpack.FinishedPlot.Value == true then
				print(TotalPlayers[n],"is true!")
				n.Team = game.Teams.Playing
				print(TotalPlayers[n],"is playing!")
			else
				print(TotalPlayers[n],"isn't true.")
				n.Team = game.Teams.Spectating
				print(TotalPlayers[n],"won't be playing with us today.")
			end
		end
		----------------------------------------------------------------------PROBLEM
	end
end)

It also does not give back any error at all. I knew something was wrong when my team did not change. So I added prints to debug it, but none ended up showing, like the script had just full stopped in the middle of it.

1 answer

✓ Accepted answer

The problem is you declared the the array which contains the players at the top, which means, when the script immediately executed, there was no players in the server, so TotalPlayers is actually an empty array;

so you have to declare the TotalPlayers variable in the code block where it's used, this way so Players:GetChildren() returns the updated player list




local GameStatus = workspace.GameStatus
local HeadingCountdown = workspace.HeadingCountdown
local IntermissionTime = 30
local PlottingTime = 60
local PlotEvent = workspace.PlotEvent
local Players = game.Players

GameStatus.Changed:Connect(function()

    if GameStatus.Value == "Intermission" then
        for i = IntermissionTime,0,-1 do --30 being the main value
            HeadingCountdown.Value = i
            print(i)
            wait(1)
        end
        GameStatus.Value = "Plotting"
        print(GameStatus,"is",GameStatus.Value,"on script")



    elseif GameStatus.Value == "Plotting" then
	local TotalPlayers = Players:GetChildren()

        print("WE GOT IT")
        PlotEvent:FireAllClients()
        for i = PlottingTime,0,-1 do
            HeadingCountdown.Value = i
            print(i)
            wait(1)
        end
        print("starting count")
        wait(3)
        
        for i,player in pairs(TotalPlayers) do
            print(player.Name,"is up!")
            if player.Backpack.FinishedPlot.Value == true then
                print(player.Name," is true!")
                Player.Team = game.Teams.Playing
                print(player.Name," is playing!")
            else
                print(player," isn't true.")
                player.Team = game.Teams.Spectating
                print(player.Name," won't be playing with us today.")
            end
        end

    end
end)

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