local Players = {}
Players.Player1 = "Open"
Players.Player2 = "Open"
Players.Player3 = "Open"
Players.Player4 = "Open"
game.Players.PlayerAdded:Connect(function(Player)
print(Player)
for _,p in pairs(Players) do
if p == "Open" then
p = Player -- Value is still "Open" instead of Player. Help.
for i,v in pairs(Players) do print(i,v) end
break
end
end
end)
You’re changing the variable p
instead of the reference Players
.
Try changing the index of the reference Players
.
Players[playerName] = Player
Thank you for the tip. I fixed it:
local Players = {}
Players[1] = "Open"
Players[2] = "Open"
Players[3] = "Open"
Players[4] = "Open"
game.Players.PlayerAdded:Connect(function(Player)
print(Player)
for i=1,4,1 do
if Players[i] == "Open" then
Players[i] = Player
for i,v in pairs(Players) do print (i,v) end
break
end
end
end)
Heads up, and I get this is a particular thing and you will see the way you have done something used in many places but coming from someone who solo’s a HUGE database. I suggest when you use pairs.
for i, v in pairs(dictionary)
Don’t EVER use single variable names unless you are writing an extremely generic function.
So you would replace this with.
for playerIndex, Player in pairs(Players)
– The reason you do this, is you would be VERY surprised with how much cognative load you spend when you have to “figure your code out.” even if you have done it 1000x if you dont look at it long enough and look back you will spend time thinking through it whcih spends cognative focus which is a finite resource per a day.
By taking some time to be more literal with your variables you can make it considerably easier to understand what is going on and what counts as what.
This is up to you, but I promise you. When your code base gets over 40 to 50k lines that your having to bounce around in you will thank yourself for making it faster.
In general, only use single letter variables ONLY for HIGH utilitarian generic
Also I suggest not doing direct connections but instead to follow this pattern
This will help have a clear idea of WHAT you were doing here as your connecting to an event and atm while you could ASSUME the only thing your doing or you only care about this in one context that is in general not a great practice for event binds. Now I am not a saint and I generally only do this pattern if the event is hooked to something pretty major if its a minor bind or action based on an event i think that practice is fine with some commenting to give context but Just suggestions that will help your code be more readable.
Also, I get the whole habit of wanting to single line your code out but it does not save you much speed and its more for personal feeling then really readable one liners are generally NOT as readable.
I get it, this is just a test thing, but just thought i would just make some suggestions =D.
Write like your future self IS your other coder on your team.
local function OnPlayerConnect(Player)
print(Player)
for i=1,4,1 do
if Players[i] == "Open" then
Players[i] = Player
for i,v in pairs(Players) do
print (i,v)
end
break
end
end
game.Players.PlayerAdded:Connect(OnPlayerConnect)