Hello! I am making a game and when the round ends I want to give all players on the Survivors team cash. I have been trying to figure it out for a few hours now and am kinda stumped. Thanks for the help!
It depends on how you have your coins system set up.
If you’re using leaderstats then you could do something like this:
function giveTeamCoins(teamColor,amount)
for i,v in ipairs(game.Players:GetChildren()) do
if v.TeamColor == BrickColor.new(teamColor) then
v.leaderstats.Coins.Value = v.leaderstats.Coins.Value + amount
end
end
end
giveTeamCoins('Light Blue',100)
Thanks so much for the help! It worked perfectly!
You can do
local GetPlayers = RedTeam:GetPlayers()
--> Assigning it as a local variable is faster.
for _, Target in ipairs(GetPlayers) do
--> This returns an array of every player in the specified team.
print(Target.Name)
end
yea, I tried to write code that was more general purpose for learning and future uses
-
You used pairs over ipairs, ipairs is designed to be used in arrays.
-
GetChildren returns every object that is in game.Players,
not just players (if anything is put there). -
GetChildren and GetPlayers both returns an array; just stating that.
-
You don’t need this to be a global function, local functions work fine in this case.
yea you’re totally right about the ipairs thing, should’ve used ipairs
you’re also right about making things local
I could have used game.Players:GetPlayers() but GetChildren is more general purpose and better to practice with at first.
Don’t teach them the past; teach them the future.
What if he was experimenting and wanted to do something outside of game.Players?
workspace:GetPlayers() wouldn’t be much use to him then
It’s better to practice with something that’s more general purpose than to use something highly specific when you’re starting out
This is not the case. You don’t do that for players since GetPlayers() exist.
let’s take this to the PMs, we’re spamming Scripting Support