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

Script is ignoring my for loop, but the one after it is not?

Hello Everyone!

To see if my script for cloning a tool to every player's backpack was working, I edited the for loop that puts a sword into the player's backpack to see what my "player" variable was referencing to (I'm still learning to code. I also included other for loops nested in a while true do loop in the code snippet to see if it had the problem had to do with this problem).

Main = require(game.Workspace.ModuleScripts.Main)
local status = game.ReplicatedStorage:WaitForChild("Status")
local intermissionTime = 3
local roundTime = 120
local players = game.Players:GetPlayers()
local playerService = game:GetService("Players")
local Number = intermissionTime

local sword = game:GetService("ReplicatedStorage").Swords.BasicSword 

while true do
	for i = 1, intermissionTime, 1 do
		status.Value = "INTERMISSION (" .. Number .. ")"
		wait(1)
		Number = Number - 1
	end
	Number = roundTime
	Main.ToBattleMap()
	
	for i = 1, #players, 1 do -- for loop that is completely ignored
		player = players[i]
		print(player.Parent)
	end
	
	for i = 1, roundTime, 1 do
		status.Value = "ROUND IN PROGRESS (" .. Number .. ")"
		wait(1)
		Number = Number - 1
	end
	Main.ToLobbyMap()
	Number = intermissionTime
	
end

When I went to test it out, it didn't print anything, but the last for loop worked. Can someone please tell me why this is happening and how to fix it?

Thanks in advance :)

1 comment

0

To make this easier to analyze do you mind simplifying the script by instead reassigning variables like local intermission time is 3 but say intermission time is equal to a new variable. Can you keep it as one to make it simpler please?

2 answers

✓ Accepted answer

Your script is setting the players variable once when it first starts, but when run online there won't be any players on the server when the script first starts. You should move the assignment of this variable into the loop so that it is checked every round, and you should probably check how many players are in the server before starting the round. Something like this might work for you:

-- Not showing the initialization of the rest of your variables here

-- Set this to minimum amount of players needed to start round
local minPlayersNeeded = 2

while true do
	for i = 1, intermissionTime, 1 do
		status.Value = "INTERMISSION (" .. Number .. ")"
		wait(1)
		Number = Number - 1
	end
	Number = roundTime
	Main.ToBattleMap()
	
	local players = game:GetService("Players"):GetPlayers()
	if #players >= minPlayersNeeded then
		for i = 1, #players, 1 do -- for loop that is completely ignored
			player = players[i]
			print(player.Parent)
		end
	
		for i = 1, roundTime, 1 do
			status.Value = "ROUND IN PROGRESS (" .. Number .. ")"
			wait(1)
			Number = Number - 1
		end
		Main.ToLobbyMap()
		Number = intermissionTime
	end
end
Main = require(game.Workspace.ModuleScripts.Main)
local status = game.ReplicatedStorage:WaitForChild("Status")
local intermissionTime = 3
local roundTime = 120
local players = game:GetService("Players"):GetPlayers() --get players through this instead
local Number = intermissionTime

local sword = game:GetService("ReplicatedStorage").Swords.BasicSword 

while true do
    for i = 1, intermissionTime, 1 do
        status.Value = "INTERMISSION (" .. Number .. ")"
        wait(1)
        Number = Number - 1
    end
    Number = roundTime
    Main.ToBattleMap()

    for i = 1, #players, 1 do -- for loop that is completely ignored
        player = players[i]
        print(player.Parent)
    end

    for i = 1, roundTime, 1 do
        status.Value = "ROUND IN PROGRESS (" .. Number .. ")"
        wait(1)
        Number = Number - 1
    end
    Main.ToLobbyMap()
    Number = intermissionTime

end

If Main.ToBattleMap() worked as desired with no errors, then the problem had to be how you were getting the players. I have changed this to an alternate way, but I can't actually test this right now myself, so let me know if it doesn't work.

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