Hi all!
To keep it brief, I’m trying to update a sort of player list when someone joins (but not leaves) the game
The code is in a server Script in Workplace, in case that is the issue.
(An example of the GUI before running, Don’t worry about Other Player 4 not having an image)
The main problem is that It’s working, albeit inconsistently. When I run a server test with 1-2 players, it seems to work fine. 3-4 players, however, and it seems to not update at all when a player joins. I store the players into a dictionary and then get the text versions of the names using tostring() to put them into the corresponding text labels
Example of it working with 2 players: (This is the result for both players in the server)
Example of it NOT working with 4 players: (For Player1 and Player2 it only prints the Player1 and Player2 GUI. For Player4, It prints every players GUI like it should be doing)
--Starting values for the table
local Survivors = {
Survivor1 = "No One" ,
Survivor2 = "No One" ,
Survivor3 = "No One" ,
Survivor4 = "No One"
}
local Players = game:GetService("Players")
local StarterGUI = game:GetService("StarterGui")
-- Player 1 HUD
local otherPlayerTextOne = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer1")
local otherPlayerBlotOne = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer1Blot")
local otherPlayerStatusOne = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerStatus1")
local otherPlayerLabelOne = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerLabel1")
-- Player 2 HUD
local otherPlayerTextTwo = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer2")
local otherPlayerBlotTwo = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer2Blot")
local otherPlayerStatusTwo = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerStatus2")
local otherPlayerLabelTwo = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerLabel2")
-- Player 3 HUD
local otherPlayerTextThree = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer3")
local otherPlayerBlotThree = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer3Blot")
local otherPlayerStatusThree = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerStatus3")
local otherPlayerLabelThree = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerLabel3")
-- Player 4 HUD
local otherPlayerTextFour = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer4")
local otherPlayerBlotFour = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayer4Blot")
local otherPlayerStatusFour = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerStatus4")
local otherPlayerLabelFour = StarterGUI:FindFirstChildOfClass("ScreenGui"):FindFirstChild("UIDragger"):FindFirstChild("OtherPlayerLabel4")
--Checks to see if the Player is a survivor, since I'm early in development, this is more of an abstraction for now
local isSurvivor = true
local amountOfSurvivors = 0
--Checks to see if Survivor slot 1 is taken or not
function isOneEnabled()
if Survivors["Survivor1"] == "No One" then
otherPlayerTextOne.Transparency = 1
otherPlayerBlotOne.ImageTransparency = 1
otherPlayerStatusOne.ImageTransparency = 1
otherPlayerLabelOne.ImageTransparency = 1
else
otherPlayerTextOne.Transparency = 0
otherPlayerBlotOne.ImageTransparency = 0
otherPlayerStatusOne.ImageTransparency = 0
otherPlayerLabelOne.ImageTransparency = 0
end
end
--Checks to see if Survivor slot 2 is taken or not
function isTwoEnabled()
if Survivors["Survivor2"] == "No One" then
otherPlayerTextTwo.Transparency = 1
otherPlayerBlotTwo.ImageTransparency = 1
otherPlayerStatusTwo.ImageTransparency = 1
otherPlayerLabelTwo.ImageTransparency = 1
else
otherPlayerTextTwo.Transparency = 0
otherPlayerBlotTwo.ImageTransparency = 0
otherPlayerStatusTwo.ImageTransparency = 0
otherPlayerLabelTwo.ImageTransparency = 0
end
end
--Checks to see if Survivor slot 3 is taken or not
function isThreeEnabled()
if Survivors["Survivor3"] == "No One" then
otherPlayerTextThree.Transparency = 1
otherPlayerBlotThree.ImageTransparency = 1
otherPlayerStatusThree.ImageTransparency = 1
otherPlayerLabelThree.ImageTransparency = 1
else
otherPlayerTextThree.Transparency = 0
otherPlayerBlotThree.ImageTransparency = 0
otherPlayerStatusThree.ImageTransparency = 0
otherPlayerLabelThree.ImageTransparency = 0
end
end
--Checks to see if Survivor slot 4 is taken or not
function isFourEnabled()
if Survivors["Survivor4"] == "No One" then
otherPlayerTextFour.Transparency = 1
otherPlayerBlotFour.ImageTransparency = 1
otherPlayerStatusFour.ImageTransparency = 1
otherPlayerLabelFour.ImageTransparency = 1
else
otherPlayerTextFour.Transparency = 0
otherPlayerBlotFour.ImageTransparency = 0
otherPlayerStatusFour.ImageTransparency = 0
otherPlayerLabelFour.ImageTransparency = 0
end
end
-- Prints out the Survivors of the game
function whoAreSurvivors()
print(Survivors["Survivor1"])
print(Survivors["Survivor2"])
print(Survivors["Survivor3"])
print(Survivors["Survivor4"])
end
--[[
The main issue is in this code, it should be updating every time a player joins, it loops through the players and puts their Player into the dictionary.
]]--
local function onPlayerAdded(child)
for i, child in pairs(Players:GetChildren()) do
for survNum,name in pairs(Survivors) do
--Checks amount of survivors/players
if amountOfSurvivors < 4 then
--[[
Checks to see if the Players name is already in the dictionary if it's the dictionary, it breaks out of the loop. If there's another name in the current index, it will move on to the next survivor slot
]]--
if child.Name == name or name ~= "No One" then
if child.Name == tostring(name) then break
elseif name ~= "No One" then
print("Whoops")
print("Sorry ".. child.name .. ", " .. survNum .. " is taken by " .. tostring(name) )
end
else
name = game:GetService("Players"):FindFirstChild(child.name)
print("YES")
print(survNum .. " Is now... ".. child.name)
--Makes the current index equal to the Players player, not the players name
Survivors[survNum] = game:GetService("Players"):FindFirstChild(child.name)
print(survNum, name)
amountOfSurvivors = amountOfSurvivors + 1
break
end
end
end
--Sets the text of the the GUIs to the string versions of the Players player
otherPlayerTextOne.Text = tostring(Survivors["Survivor1"])
otherPlayerTextTwo.Text = tostring(Survivors["Survivor2"])
otherPlayerTextThree.Text = tostring(Survivors["Survivor3"])
otherPlayerTextFour.Text = tostring(Survivors["Survivor4"])
--Checks to see if the Survivor slot is occupied or empty
isOneEnabled()
isTwoEnabled()
isThreeEnabled()
isFourEnabled()
whoAreSurvivors()
end
end
--Runs the function again for players already in the game
game.Players.PlayerAdded:Connect(onPlayerAdded)
for _,child in pairs(game.Players:GetPlayers()) do
onPlayerAdded(child)
end
I’ve tried many different onPlayerAdded functions but none seem to work for me, I’ve also looked though the dev forum and documentations to try and understand PlayerAdded more comprehensively, but I can’t seem to grasp it all that well
Any help would be greatly appreciated, I’m new to scripting so I’m sorry if the issue isn’t very complex or if you’ve seen it before, have a nice day!