Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to add cash to all members of a team?

I'm using the default Roblox CTF script (which could be found in the CTF Template), and I made an IntValue in leaderstats called Cash. I found out how to add cash to the player who succesfully captured the flag, but I want to be able to add cash to all the players of the team who scored a point. The part where the script adds the point is on lines 116 - 120. Here's the script:

local TeamManager = {}

-- ROBLOX services
local Teams = game.Teams
local Players = game.Players

-- Game services
local Configurations = require(game.ServerStorage.Configurations)
local DisplayManager = require(script.Parent.DisplayManager)

-- Local variables

local TeamPlayers = {}
local TeamScores = {}

-- Initialization

for _, team in ipairs(Teams:GetTeams()) do
	TeamPlayers[team] = {}
	TeamScores[team] = 0
end

-- Local Functions

local function GetTeamFromColor(teamColor)
	for _, team in ipairs(Teams:GetTeams()) do
		if team.TeamColor == teamColor then
			return team
		end
	end
	return nil
end

-- Public Functions

function TeamManager:ClearTeamScores()
	for _, team in ipairs(Teams:GetTeams()) do
		TeamScores[team] = 0
		DisplayManager:UpdateScore(team, 0)
	end
end

function TeamManager:HasTeamWon()
	for _, team in ipairs(Teams:GetTeams()) do
		if TeamScores[team] >= Configurations.CAPS_TO_WIN then
			return team
		end
	end
	return false
end

function TeamManager:GetWinningTeam()
	local highestScore = 0
	local winningTeam = nil
	for _, team in ipairs(Teams:GetTeams()) do
		if TeamScores[team] > highestScore then
			highestScore = TeamScores[team]
			winningTeam = team
		end
	end
	return winningTeam
end

function TeamManager:AreTeamsTied()
	local teams = Teams:GetTeams()
	local highestScore = 0
	local tied = false
	for _, team in ipairs(teams) do
		if TeamScores[team] == highestScore then
			tied = true
		elseif TeamScores[team] > highestScore then
			tied = false
			highestScore = TeamScores[team]
		end
	end
	return tied
end

function TeamManager:AssignPlayerToTeam(player)
	local smallestTeam
	local lowestCount = math.huge
	for team, playerList in pairs(TeamPlayers) do
		if #playerList < lowestCount then
			smallestTeam = team
			lowestCount = #playerList
		end
	end
	table.insert(TeamPlayers[smallestTeam], player)
	player.Neutral = false
	player.TeamColor = smallestTeam.TeamColor
end

function TeamManager:RemovePlayer(player)
	local team = GetTeamFromColor(player.TeamColor)
	local teamTable = TeamPlayers[team]
	for i = 1, #teamTable do
		if teamTable[i] == player then
			table.remove(teamTable, i)
			return
		end
	end
end

function TeamManager:ShuffleTeams()
	for _, team in ipairs(Teams:GetTeams()) do
		TeamPlayers[team] = {}
	end
	local players = Players:GetPlayers()
	while #players > 0 do
		local rIndex = math.random(1, #players)
		local player = table.remove(players, rIndex)
		TeamManager:AssignPlayerToTeam(player)
	end
end

function TeamManager:AddTeamScore(teamColor, score)
	local team = GetTeamFromColor(teamColor)
	TeamScores[team] = TeamScores[team] + score
	DisplayManager:UpdateScore(team, TeamScores[team])
end

return TeamManager

If you are on the template, you can find this script in: game.ServerScriptService.MainGameScript.GameManager.TeamManager

2 answers

✓ Accepted answer
for i,v in pairs(game.Players:GetPlayers()) do
	if v.TeamColor == BrickColor.new("Really Red") then --change really red to brickColor of team
		v.leaderstats.Cash = v.leaderstats.Cash + 100 --amount to give
	end
end

2 comments

0

But how will I make it when Blue captures, everyone in blue gets +100, and when Red captures, everyone in red gets +100

0

You could change the brick color that it checks to a variable that you give when you call your AddTeamScore function.

Nevermind, I fixed the script based off the first answer. Here's what I put:

function TeamManager:AddTeamScore(teamColor, score)
	local team = GetTeamFromColor(teamColor)
	TeamScores[team] = TeamScores[team] + score
	DisplayManager:UpdateScore(team, TeamScores[team])
	for i,v in pairs(game.Players:GetPlayers()) do
    if v.TeamColor == teamColor then --change really red to brickColor of team
        v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + 100 --amount to give
    end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.