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:
I then after each round reset the names back to normal with a value in each poster named “OriginalName”
Second time running the code:
Third time:
Fourth time:
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