How to make IF Players.Team = true the cash value increases by a certain amount?

Im trying to make it to where if a Players team is not a citizen. the cash value + rate, will change (basically if a player has a Job the pay rate will increase)

This is where the pay rate is in the script
while true do
** wait(2)**
** Cash.Value = Cash.Value + 1**
** end**
end

Here is the script (free leaderstats script!)

local DataStoreService = game:GetService("DataStoreService")
local PlayerCash = DataStoreService:GetDataStore("PlayerCash")

function OnPlayerAdded(player)
	local stats = Instance.new("Folder", player)
	stats.Name = 'leaderstats'
	local Cash = Instance.new("NumberValue", stats)
	Cash.Name = "Money"
	Cash.Value = 350000
	local DataLoaded = Instance.new("BoolValue", player)
	DataLoaded.Name = "CashDataLoaded"
	DataLoaded.Value = false

	local data
	local attempts = 10

	repeat
		local success, err = pcall(function()
			data = PlayerCash:GetAsync(player.UserId)
		end)
		if success then
			print("Success!")
			DataLoaded.Value = true
			break
		else
			warn("Was problem with datastore! (Attempts left ", attempts, ")")
		end
		wait(1)
		attempts = attempts - 1
	until attempts <= 0

	if not DataLoaded.Value then
		warn("Player " .. player.Name .. "'s cash data has not been loaded!")
		-- player:Kick("Couldn't load your data, try to rejoin later")
		return
	end

	if data then
		Cash.Value = data
	end
	while true do
		wait(2)
		Cash.Value = Cash.Value + 1
	end
end

function OnPlayerRemoving(player)
	if player:FindFirstChild("CashDataLoaded") and player.CashDataLoaded.Value == true then
		local attempts = 10

		repeat
			local success, err = pcall(function()
				PlayerCash:SetAsync(player.UserId, player.leaderstats:FindFirstChild("Money").Value)
			end)
			if success then
				print("Success!")
				break
			else
				warn("Was problem with datastore! (Attempts left ", attempts, ")")
			end
			wait(1)
			attempts = attempts - 1
		until attempts <= 0
		if attempts <= 0 then
			warn("Player " .. player.Name .. "'s cash data has not been saved!")
		end
	end
end


game.Players.PlayerAdded:Connect(OnPlayerAdded)
game.Players.PlayerRemoving:Connect(OnPlayerRemoving)

Here is the Teams
pic2

1 Like

First, create a variable for the pay rate named something like PayRate. Now, you just want to add the PayRate to the cash amount and change the PayRate depending on the player’s team.

PayRate = 1

while true do
	wait(2)

	if player.Team == game.Teams.Citizen then -- Sets pay rate if player is a citizen
		PayRate = 1
	else
		PayRate = 2 -- default job pay rate

		if player.Team == game.Teams["Name of the job"] then -- Pay rate for a specific job
			PayRate = 5
		elseif player.Team == game.Teams["Another job"] then -- You can keep adding elseifs for specific jobs 
			PayRate = 10
		end
	end

	Cash.Value = Cash.Value + PayRate
end

Did not work I hope there is another solution.

Seems like if all teams have the same color, they won’t be separated. This means the player will stay on one team no matter how you change it. Also, if the name is not valid here, that will result in an error:

if player.Team == game.Teams["Name of the job"] then
   PayRate = 2
end

image

So make sure all the names in the if statements are valid and each team has a different color.

Make a pay rate dictionary that contains teams (names) and their pay rate:

local dictionary = {
    ["Team"] = 1; -- cash
    ["AnotherTeam"] = 2;
}

And take the value from it.

while true do
    wait(2)
    Cash.Value = Cash.Value + dictionary[your_player.Team.Name] -- searches for payrate of a certain team
end
1 Like

Will this be in the script or a module script?

= is an assignment operator. == is an equality operator.

That script can be copy-pasted into your leaderstats script. Just make sure you replace the while true do part with @EVALDOL’s code. Also, put the dictionary somewhere before the loop. (or actually, you can put it in it if you want to)

I would not use repeat for getting or saving data. I rewrote that.
Add this new code
Todo What you should do is then for each team add an Attribute called PayRate must be of number type.
image

local DataStoreService = game:GetService("DataStoreService")
local PlayerCash = DataStoreService:GetDataStore("PlayerCash")

function OnPlayerAdded(player)
	local stats = Instance.new("Folder", player)
	stats.Name = 'leaderstats'
	local Cash = Instance.new("NumberValue", stats)
	Cash.Name = "Money"
	Cash.Value = 350000

	local success, err = pcall(function()
		local cash = PlayerCash:GetAsync(player.UserId)
		if cash then
			-- If not cash then this is a new player
			Cash.Value = cash 
		end
	end)
	if not success then
		error("Datastore GetAsync! for player: "..player.Name.. " Error Message: "..err)
	end

	local payRate = 1

	player:GetPropertyChangedSignal("Team"):Connect(function()
        -- If team changes then it gets Attribute of the new team 
		payRate = player.Team:GetAttribute("PayRate")
	end)

	while true do
		wait(2)
		Cash.Value = Cash.Value + payRate
	end
end

function OnPlayerRemoving(player)
	local success, err = pcall(function()
		PlayerCash:SetAsync(player.UserId, player.leaderstats:FindFirstChild("Money").Value)
	end)
	if not success then
		error("Datastore SetAsync! for player: "..player.Name.. " Error Message: "..err)
	end
end


game.Players.PlayerAdded:Connect(OnPlayerAdded)
game.Players.PlayerRemoving:Connect(OnPlayerRemoving)
1 Like

Did not work or previous comments.

Why don’t these work? could you copy output logs if there are any errors or warnings? Did anything different happen?

All of these methods should work, so the problem is somewhere else. Are you sure teams have a different Team.TeamColor? Also, as @gertkeno said, you could copy or screenshot the output. Maybe there is an error in the script before the loop.

1 Like

Did you enable studio api access?


Here is place file I tested everything and it works.
PayrRate.rbxl (35.4 KB)

1 Like

All the teams have a different color, and it has access to API Services.
When the player presses the textbutton it changes the team but the payrate dosent change.

(I can show a video if needed)

You should show the output. All errors if there is.

Are you changing the player’s team on the server? If not, that’s the problem. Changing it in a Local Script only changes it on the client but it will still look like you are on the selected team. To change the player’s team on the server, use a RemoteEvent.

Im changing the players team on the server (serverscript),
Heres a video of when the team changes with output, but the payrate dosent change.

Looking at the scroller of the output window, I would assume that not the whole output is shown in the video. Can you please screenshot/copy-paste the whole output?

I searched for the script in the output and no errors or anything were shown, and there is no errors in it

Are you using a RemoteEvent to change the player’s team? If yes, then you may have a problem with that script, maybe the team on the server is set to nil or doesn’t change at all. Can you show the scripts responsible for changing the player’s team?