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

How do I make certain teams earn money?

Hello I need help on what to do, so far I have tried to put the money earning scripts in the teams, but it doesn't work. Here is the script:

local playerStats = {} --this keeps a list of the stats for each player that enters the game

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderstats) --We instance a new IntValue as a child of 'leaderstats'
    money.Name = "Money" --this is the name you want the leader-stat to be when it shows up in-game.
    money.Value = 0 --this is the value of money the new player starts out with. To change this, you can add some more code (shown later)
end)
local money = Instance.new("IntValue", leaderstats)
    money.Name = "Money"
    money.Value = 200

    stats[player] = leaderstats 
end)

while true do --infinite loop
    for player, stats in ipairs(playerStats) do --loops through each player => stat pair in the table
        stats.Money.Value = stats.Money.Value + 2
    end
    wait(5)
end

I would like an actual script for this or a solution if your answering this. Thanks.

2 answers

✓ Accepted answer

Well, one way of doing it is by creating a CheckTeams function. The parameter could be the Player, or the TeamColor. Within the function, there would be an if statement, as well as elseif statements, that checks the TeamColor, and gives them the cash. Here's an example:

function CheckTeams(player)
	if player.TeamColor == BrickColor.new("Really red") then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value + 2
	end
end

All you need to do is put that into your current script, add the teams, properly add the money, and for the 'for' loop you have at the end, just have it go through the Players, not the stats.

2 comments

0

Thanks for all the answers I really appreciate it, hopefully it will work now. I have accepted both answers and now you will receive reputation

0

Okay so I have tried both scripts ImageLabel's one and Shawnyg's one, neither of them work sadly, I did put the teams properly and I did put both of them in workspace and ServerScriptService for tests and so far they haven't worked I still appreciate the answers and hopefully I will get this sorted for my game. Thanks anyway

local playerStats = {} 
local teamSalary = {
	["Really red"] = 200;
	["Purple"] = 100;
	["White"] = 0;
};

local function fetchSalary (teamColor)
	if teamColor and BrickColor.new(teamColor) then
		for team, salary in ipairs(teamSalary) do
			if team:upper() == teamColor:upper() then
				return salary
			else
				return 0
			end 
		end 
	end
end

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("NumberValue")
	money.Parent = leaderstats
	money.Name ="Money"
	
	playerStats[player] = leaderstats
end)

while wait(5) do
	for player, stats in pairs(playerStats) do
		if game.Players:FindFirstChild(player) then
			local user = game.Players:FindFirstChild(player) 
			local pay = fetchSalary(tostring(user.TeamColor))
			stats.Money.Value = stats.Money.Value + pay
		end
	end
end

Put it in ServerScriptService or whatever other service will run it smoothly. You shouldn't parent it to each team.

I haven't tested it but it basically awards players a certain amount of money based on their team. In the table teamSalary, set all TeamColors and the amount you want them to receive.

the function fetchSalary iterates through the table teamSalary and returns the appropriate value based on TeamColor, or 0.

in the playerAdded event, the leaderstats are created and each player is basically assigned a slot in the playerStats table.

Finally, the while loops increments the value of Money, from the table playerStats based on the assigned value, and the presence of the player.


I have not tested the code and can't confirm that it will be 100% functional. I don't have ROBLOX Studio on my Chromebook and therefore cannot test it until I get home.

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