'i' in for loop not resetting?

Hello developers,

I am trying to make a system in my game that adds the images of all of the players in the game on some posters in the main map area when a round is in progress. Part of the system is renaming the posters to the names of the players that are on them. When I did a test of the system, the first repetition of the code worked fine, but the second, third, fourth, etc times did not work as expected. It is easier to just show some images of what happened, so here are the screenshots.

First repetition of the code:
Screen Shot 2023-08-28 at 6.34.47 PM

I then after each round reset the names back to normal with a value in each poster named “OriginalName”

Screen Shot 2023-08-28 at 6.35.16 PM

Second time running the code:

Screen Shot 2023-08-28 at 6.35.04 PM

Third time:

Screen Shot 2023-08-28 at 6.35.24 PM

Fourth time:

Screen Shot 2023-08-28 at 6.35.44 PM

Here is the for loop that handles the posters:

local function setPosters()
	for i, plr in pairs(playersInRound) do
		posters[tostring(i)]:WaitForChild("SurfaceGui").ImageLabel.Image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size420x420)
		posters[tostring(i)].Name = (plr.Name)
		
		task.wait()
	end
end

And,

for i, plr in pairs(playersInRound) do
		posters[plr.Name].SurfaceGui.ImageLabel.Image = "rbxasset://textures/ui/GuiImagePlaceholder.png"
		posters[plr.Name].Name = posters[plr.Name].OriginalName.Value

		task.wait()
end

Here is the full script if it is needed (server script):

local players = game.Players


--services
local RP = game:GetService("ReplicatedStorage")

local murder
local psychic
local surviors = {}
local playersInRound = {}

local posters = workspace.Posters

local function decideRoles()
	for i, plr in pairs(players:GetChildren()) do --add all players to playersInRound
		table.insert(playersInRound, plr)
		table.insert(surviors, plr)
		task.wait()
	end
	
	if #playersInRound > 2 then
		murder = playersInRound[math.random(1, #playersInRound)] --Pick a random person to be the murderer
		local find1 = table.find(surviors, murder)
		if find1 then table.remove(surviors, find1) end
		
		print("The murder is: "..murder.Name)

		psychic = surviors[math.random(1, #surviors)] -- pick random person to be psychic
		local find2 = table.find(surviors, psychic)
		if find2 then table.remove(surviors, find2) end
		
		print("The psychic is: "..psychic.Name)
	else
		print("Three players are needed to start the game")
	end
	
end

local function setPosters()
	for i, plr in pairs(playersInRound) do
		posters[tostring(i)]:WaitForChild("SurfaceGui").ImageLabel.Image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size420x420)
		posters[tostring(i)].Name = (plr.Name)
		
		task.wait()
	end
end

task.wait(5)

while true do
	wait(10)
	decideRoles()
	setPosters()
	wait(10)
	
	for i, plr in pairs(playersInRound) do
		posters[plr.Name].SurfaceGui.ImageLabel.Image = "rbxasset://textures/ui/GuiImagePlaceholder.png"
		posters[plr.Name].Name = posters[plr.Name].OriginalName.Value

		task.wait()
	end
	
	for i, plr in pairs(playersInRound) do
		local find = table.find(playersInRound, plr)
		if find then table.remove(playersInRound, find) end
		
		local find2 = table.find(surviors, plr)
		if find2 then table.remove(surviors, find2) end
	end
	
end

players.PlayerRemoving:Connect(function(player) -- if player leaves, remove them from playersInRound
	posters[player.Name].Name = posters[player.Name].OriginalName.Value
	
	local find = table.find(playersInRound, player)
	if find then table.remove(playersInRound,find) end
end)

Could anyone help?

My best guess is that it is something to do with ‘i’ and it possibly not resetting, however I am unsure.

THERE ARE NO ERRORS IN THE OUTPUT

2 Likes

Have you tried using :WaitForChild(i) instead of indexing it using [i]?

Code:

--//Services
local players = game.Players
local RP = game:GetService("ReplicatedStorage")

--//Variables
local posters = workspace.Posters

--//Controls
local murder = nil
local psychic = nil

--//Tables
local survivors = {}
local playersInRound = {}

--//Functions
local function decideRoles()
	for i, plr in ipairs(players:GetPlayers()) do --add all players to playersInRound
		table.insert(playersInRound, plr)
		table.insert(survivors, plr)
	end

	if #playersInRound > 2 then
		murder = playersInRound[math.random(#playersInRound)] --Pick a random person to be the murderer
		local find1 = table.find(survivors, murder)
		if find1 then table.remove(survivors, find1) end

		print("The murder is: "..murder.Name)

		psychic = survivors[math.random(#survivors)] -- pick random person to be psychic
		local find2 = table.find(survivors, psychic)
		if find2 then table.remove(survivors, find2) end

		print("The psychic is: "..psychic.Name)
	else
		print("Three players are needed to start the game")
	end
end

local function setPosters()
	for i, plr in ipairs(playersInRound) do
		local poster = posters:WaitForChild(i)
		poster.Name = plr.Name
		poster:WaitForChild("SurfaceGui").ImageLabel.Image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size420x420)
	end
end

task.wait(5)

while task.wait(10) do
	decideRoles()
	setPosters()
	task.wait(10)
	
	for i, poster in ipairs(posters:GetChildren()) do
		poster.Name = poster.OriginalName.Value
		poster.SurfaceGui.ImageLabel.Image = "rbxasset://textures/ui/GuiImagePlaceholder.png"
	end

	for i, plr in ipairs(playersInRound) do
		local find = table.find(playersInRound, plr)
		if find then table.remove(playersInRound, find) end

		local find2 = table.find(survivors, plr)
		if find2 then table.remove(survivors, find2) end
	end
end

players.PlayerRemoving:Connect(function(player) -- if player leaves, remove them from playersInRound
	local poster = posters:WaitForChild(player.Name)
	poster.Name = poster.OriginalName.Value

	local find = table.find(playersInRound, player)
	if find then table.remove(playersInRound,find) end
end)
1 Like

Used that code and got the same bug

Screen Shot 2023-08-28 at 7.08.22 PM

1 Like

Maybe try using table.clear on the tables at the end?

Code:

--//Services
local players = game.Players
local RP = game:GetService("ReplicatedStorage")

--//Variables
local posters = workspace.Posters

--//Controls
local murder = nil
local psychic = nil

--//Tables
local survivors = {}
local playersInRound = {}

--//Functions
local function decideRoles()
	for i, plr in ipairs(players:GetPlayers()) do --add all players to playersInRound
		table.insert(playersInRound, plr)
		table.insert(survivors, plr)
	end

	if #playersInRound > 2 then
		murder = playersInRound[math.random(#playersInRound)] --Pick a random person to be the murderer
		local find1 = table.find(survivors, murder)
		if find1 then table.remove(survivors, find1) end

		print("The murder is: "..murder.Name)

		psychic = survivors[math.random(#survivors)] -- pick random person to be psychic
		local find2 = table.find(survivors, psychic)
		if find2 then table.remove(survivors, find2) end

		print("The psychic is: "..psychic.Name)
	else
		print("Three players are needed to start the game")
	end
end

local function setPosters()
	for i, plr in ipairs(playersInRound) do
		local poster = posters:WaitForChild(i)
		poster.Name = plr.Name
		poster:WaitForChild("SurfaceGui").ImageLabel.Image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.AvatarThumbnail, Enum.ThumbnailSize.Size420x420)
	end
end

task.wait(5)

while task.wait(10) do
	decideRoles()
	setPosters()
	task.wait(10)

	for i, poster in ipairs(posters:GetChildren()) do
		poster.Name = poster.OriginalName.Value
		poster.SurfaceGui.ImageLabel.Image = "rbxasset://textures/ui/GuiImagePlaceholder.png"
	end
	
	table.clear(playersInRound)
	table.clear(survivors)
end

players.PlayerRemoving:Connect(function(player) -- if player leaves, remove them from playersInRound
	local poster = posters:WaitForChild(player.Name)
	poster.Name = poster.OriginalName.Value

	local find = table.find(playersInRound, player)
	if find then table.remove(playersInRound,find) end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.