Explaining it by itself is confusing, so I’ll explain it in examples and a gif.
Say 3 players join the game, ‘Timtim’ joins first, ‘Steinf’ joins second, and ‘Timothy_Newell’ joins third. The code, I thought, should then give an output of something like this, this is what I want: https://gyazo.com/60b2bf2a56b3f3c5b0c9cc6a03148c95
Instead, since ‘Timtim’ joined first he is the only one on the leaderboard on his screen. Then, since ‘Steinf’ joined second, he and ‘Timtim’ are the only ones on the leaderboard for his screen. And lastly, since ‘Timothy_Newell’ joined third, all three of the players are on the leaderboard for his screen.
At first I thought it was an update problem, and the leaderboard needed to be updated. So I setup a ‘while loop’ just to test if that was it, and it didn’t fix it. Anyways, here’s the code, I’d greatly appreciate if someone could tell me what I did wrong, so I know what to do in the future for these kind of things.
local bodyguards = game.Teams.Bodyguards:GetPlayers()
local terrorists = game.Teams.Terrorists:GetPlayers()
local players = game.Players:GetPlayers()
local lineframe1 = script.lineframe1
local lineframe2 = script.lineframe2
local deb = false
while true do
for i = 1, #players do
wait(0.5)
local leadstats = players[i]:WaitForChild("leaderstats"):WaitForChild("Kills")
local copy = lineframe2:Clone()
copy.usernameText.Text = players[i].Name
copy.killCount.Text = players[i]:WaitForChild("leaderstats"):WaitForChild("Kills").Value
copy.LayoutOrder = i
copy.Parent = script.Parent.background.Frame.ScrollingFrame2
print("Ran")
end
end
If you are confused or need any more information, I’d happily give it out.
Okay so, since ‘Timtim’ joined first, he is the only one on the leaderboard on his screen, even when all three players are in.
Since ‘Steinf’ joined second, he and ‘Timtim’ are the only ones on the leaderboard on his screen, even when the third person joins.
I want all three players to be on the leaderboard for all their screens, no matter what order they joined in, if you’re still confused I’ll provide a few images to explain it further.
When you put the local variable outside the loop, the function is already run and stored in the local variable outside the loop, so you will only have the players who were already in the server when you join. No new players who join will get stored.
Whereas, when you use the local variable inside the loop, it is constantly updated every 0.5 seconds.
Don’t see why declaring the variable outside the loop would be better. I’m pretty sure
its more memory efficient to have it inside the loop, as it is not needed anywhere else in the code.
Ok, here you have trash graber, but when not, it will make small lags, because you have many variables, because when you redeclarate it, the old var still exist in memory. So the problem is, that you create new var every 0,5 sec.
When you will try this in c++, it will almost instantly crash.
I don’t recommend using a loop here - in fact, you shouldn’t. Teams sport event-based APIs that you should be using to your advantage. Whenever you have an event-based solution that you can work with, you should always try to choose that over using loops.
I encourage you to take a look through the Team documentation. If you notice, there are two events there: PlayerAdded and PlayerRemoved. Just like the Players service. You can use these to add players to your team menu and see instantaneous feedback as well.
Furthermore, there are general improvements to be made to your code. I can provide you a code sample which takes my suggestions and adds improvements as well. It’s just a basis since I don’t know the full extent of your code: filling in the rest or salvaging it is up to you.
local Teams = game:GetService("Teams")
local bodyguards = Teams:WaitForChild("Bodyguards")
local terrorists = Teams:WaitForChild("Terrorists")
local lineframe1 = script.lineframe1
local lineframe2 = script.lineframe2
local deb = false
local function newBodyguard(player)
-- copy a lineframe and add it to the Bodyguards scroll
end
local function leavingBodyguard(player)
-- remove their lineframe from the scroll
end
local function newTerrorist(player)
-- copy a lineframe and add it to the Terrorists scroll
end
local function leavingTerrorist(player)
-- same as leavingBodyguard but for Terrorist team
end
bodyguards.PlayerAdded:Connect(newBodyguard)
terrorists.PlayerAdded:Connect(newTerrorist)
bodyguards.PlayerRemoved:Connect(leavingBodyguard)
terrorists.PlayerRemoved:Connect(leavingTerrorist)
for _, bodyguard in ipairs(bodyguards:GetPlayers()) do
newBodyguard(bodyguard)
end
for _, terrorist in ipairs(terrorists:GetPlayers()) do
newTerrorist(terrorist)
end
You may lose the benefit of being able to rely on the index of the player from the array generated from GetPlayers (whether that’s from the Players service or the Team object), but you can resolve that problem as well in many different ways. Up to you to determine those ways.
Declaring the variable inside the loop is proper practice, not as an upvalue. You should avoid outside declarations unless something else needs the variable. In this case, only the loop is using the variable, therefore it only needs to be in the loop.
When you declare the variable inside the loop (and it’s local like this case), that variable remains local to the scope of the while loop. This means the variable cannot be accessed by code outside the while loop and it’s not apparent that it’s used anywhere outside either. This way, when the current iteration finishes, the variable is cleaned up because it goes out of scope.
It’s more of a case of practice. If you use a variable anywhere else, declare it outside. If not, declare it inside so that it can be automatically cleaned up when it goes out of scope instead of having a hanging single-use variable where it doesn’t need to be.
You don’t redeclare. When you declare the variable in the while loop, it is local to the while loop. When an iteration ends, the variable goes out of scope and gets cleaned up. I left most of my explanation above so be sure to read through that.
C++ isn’t Lua, not worth talking about here. It’s irrelevant.