So i have a flag, 2 teams ( Team 1 and Team 2 ), What i want to achieved now is when team 1 captured the flag the players under team 1 will received 50+ Gold and this will only stop if Team 2 steal the captured flag and Team 2 will received the 50+ Gold and vice versa. The problem is i don't know how to call all the members of a team.. i tried this code :
function get_team(char) local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent); if player then for _, v in pairs(game:GetService("Teams"):GetChildren()) do if v.TeamColor == player.TeamColor then TeamColor = leaderstats.Value = 100 ----- But it didn't work..
I also tried this..
function moveFlag(team, name, name2) if (team ~= owner.Value) and (owner.Value ~= neutral) then if steps.Value > 0 then a.CFrame = a.CFrame-Vector3.new(0,7/maxsteps,0) b.CFrame = b.CFrame-Vector3.new(0,7/maxsteps,0) c.CFrame = c.CFrame-Vector3.new(0,7/maxsteps,0) d.CFrame = c.CFrame-Vector3.new(0,7/maxsteps,0) steps.Value = steps.Value-1 if steps.Value == 0 then attackingteam = team defendingteam = owner.Value owner.Value = neutral if m~=nil then m:Remove() end m = Instance.new("Hint", workspace) m.Text = "Mining Resource: Sector A, controlled by " ..name2.. ", is being captured by the " ..name.. "!" if players.team == owner.Value then players.leaderstats.Credits.Value = players.leaderstats.Credits.Value + 50 end
Here is the flag script ( This is not mine but i modified some of them ) Under this flag i have BoolValue: Cap , NumberValue: CaptureDistance , BrickColorValue: CurrentOwner ( and some i don't think needs to be included )
bin = script.Parent.Base owner = script.Parent.CurrentOwner distance = script.Parent.CaptureDistance.Value steps = script.Parent.StepsToBottom maxsteps = steps.Value a = script.Parent.P4 b = script.Parent.P3 c = script.Parent.P2 d = script.Parent.P1 neutral = script.Parent.NeutralColor.Value attackingteam = nil defendingteam = nil script.Parent.FlagManager.Disabled = false players = {} for i,v in pairs(game.Players:GetPlayers()) do table.insert(players, v) end game.Players.PlayerAdded:connect(function(p) table.insert(players, p) end) function findTeamByColor(color) returnee = "" for i,v in pairs(game.Teams:children()) do if v.TeamColor == color then returnee = v.Name break end end return returnee end function moveFlag(team, name, name2) if (team ~= owner.Value) and (owner.Value ~= neutral) then if steps.Value > 0 then a.CFrame = a.CFrame-Vector3.new(0,7/maxsteps,0) b.CFrame = b.CFrame-Vector3.new(0,7/maxsteps,0) c.CFrame = c.CFrame-Vector3.new(0,7/maxsteps,0) d.CFrame = c.CFrame-Vector3.new(0,7/maxsteps,0) steps.Value = steps.Value-1 if steps.Value == 0 then attackingteam = team defendingteam = owner.Value owner.Value = neutral if m~=nil then m:Remove() end m = Instance.new("Hint", workspace) m.Text = "Mining Resource: Sector A, controlled by " ..name2.. ", is being captured by the " ..name.. "!" game:service("Debris"):AddItem(m, 10) end end elseif (team == owner.Value) and (owner.Value ~= neutral) then if steps.Value < maxsteps then a.CFrame = a.CFrame+Vector3.new(0,7/maxsteps,0) b.CFrame = b.CFrame+Vector3.new(0,7/maxsteps,0) c.CFrame = c.CFrame+Vector3.new(0,7/maxsteps,0) d.CFrame = c.CFrame+Vector3.new(0,7/maxsteps,0) steps.Value = steps.Value+1 if steps.Value == maxsteps then owner.Value = team end end elseif (team ~= owner.Value) and (owner.Value == neutral) then if team == defendingteam then if steps.Value > 0 then a.CFrame = a.CFrame-Vector3.new(0,7/maxsteps,0) b.CFrame = b.CFrame-Vector3.new(0,7/maxsteps,0) c.CFrame = c.CFrame-Vector3.new(0,7/maxsteps,0) d.CFrame = d.CFrame-Vector3.new(0,7/maxsteps,0) steps.Value = steps.Value-1 if steps.Value == 0 then if m~=nil then m:Remove() end m = Instance.new("Hint", workspace) m.Text = "Mining Resource: Sector A has been recovered from the " ..findTeamByColor(attackingteam).. " by the " ..name.. "!" game:service("Debris"):AddItem(m, 10) attackingteam = nil defendingteam = nil owner.Value = team end end else a.CFrame = a.CFrame+Vector3.new(0,7/maxsteps,0) b.CFrame = b.CFrame+Vector3.new(0,7/maxsteps,0) c.CFrame = c.CFrame+Vector3.new(0,7/maxsteps,0) d.CFrame = d.CFrame+Vector3.new(0,7/maxsteps,0) steps.Value = steps.Value+1 if steps.Value == maxsteps then if m~=nil then m:Remove() end m = Instance.new("Hint", workspace) if defendingteam then m.Text = "Mining Resource: Sector A has been captured by the " ..name.. " from the " ..findTeamByColor(defendingteam).. "!" else m.Text = "Mining Resource: Sector A has been claimed by the " ..name.. "." end game:service("Debris"):AddItem(m, 10) attackingteam = nil defendingteam = nil owner.Value = team end end end end while true do wait() for i,v in pairs(players) do if v.Character ~= nil then torso = v.Character:findFirstChild("Torso") if torso ~= nil then if (torso.Position-bin.Position).magnitude <= distance then if v.TeamColor ~= neutral then moveFlag(v.TeamColor, findTeamByColor(v.TeamColor), findTeamByColor(owner.Value)) end end end end end end
This is the only thing i needed.. Thanks
What yoy need to do is iterate through the players, find any with a matching teamcolor of whatever team you're logging, and place all players that pass the conditional into a table. You can then award each player the total amount of cash to distribute divided by how many indexes in the table there are!(:
--function to get all the members of a team, takes arguments as the team instance function getTeam(team) --table to store the players in local plrs = {} --iterate through all players for i,v in pairs(game.Players:GetPlayers()) do --check if they have the same teamcolor if v.TeamColor == team.TeamColor then --insert them into the table table.insert(plrs,v) end end --return the table return plrs end --reward all players of the team that won, say the team is Red. --The team local team = game.Teams.Red --The players on the team local plrs = getTeam(team) --Total amount of money to be distributed local totalMoney = 100 --Iterate through team players for i,v in pairs(plrs) do --Round the award money, total money divided by total players local award = math.floor((totalMoney / #plrs)+.5) --Award money v.leaderstats.Money.Value = v.leaderstats.Money.Value + award end