How to stop value adding when on a specific team

Hello!
I want to stop a value adding because I have a time script, (which adds the value for leaderstats every second)
However, i cant seem to approach this properly. I tried to add a segment checking what team the player is in inside the while loop, it has got what the player’s team is BUT when players team changes, the while loop stills sees the player’s team as the previous one.
I was thinking about using Coroutine, but I’m not sure how to resume it when it yields without using while loops on their own.
Here’s my script.

local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local tof = false
local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local times = Instance.new("IntValue")
	times.Name = "times"
	times.Value = 0
	times.Parent = leaderstats
	while tof == true do
		task.wait(1)
		times.Value += 1
	end
end

Players.PlayerAdded:Connect(leaderboardSetup)
	

I’m sorry but could you elaborate it a bit more?

I have 2 teams, and a leaderstats like time (which add every seconds). A noob team which does not get any time and a pro team which gets time. However i tried to check what team the person is in and use the if inside the loop, it doesnt work because when the player changes to pro team, the while loop stills sees the player as the noob team.

If this is your full script, then you did not set the variable tof to true. Plus, instead of setting a variable to true, you should listen to the player’s current team by Player.Team. If it is the “pro” team, then add 1 to the times value (IntValue). I haven’t tested yet, so can’t tell if it will work or not.

ignore the tof bit, I was making it where it checks the team, if its in noob its false, if its in pro its true.
it doesnt work

May I see the full code you have now?

local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local times = Instance.new("IntValue")
	times.Name = "times"
	times.Value = 0
	times.Parent = leaderstats
	while true do
		task.wait(1)
		times.Value += 1
	end
end

Players.PlayerAdded:Connect(leaderboardSetup)
	

same as before without the tof

local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local times = Instance.new("IntValue")
	times.Name = "times"
	times.Value = 0
	times.Parent = leaderstats
	while player.Team.Name == "theProTeamNameHere" do
		task.wait(1)
		times.Value += 1
	end
end

Players.PlayerAdded:Connect(leaderboardSetup)
1 Like

It doesnt work because it says index nil with name

Can you send a video of the error and with playing?

1 Like

Change the team name in the string to your team name.

1 Like

Oh i did that it gives the same erorr

Try printing out the player.Team.Name?

1 Like

ive done that before, when player changes to pro team, it prints noob team. I feel like coroutines are needed

local Players = game:GetService(“Players”)
local teams = game:GetService(“Teams”)
local tof = false
local function leaderboardSetup(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local times = Instance.new("IntValue")
times.Name = "times"
times.Value = 0
times.Parent = leaderstats
while true do
	task.wait(1)
	print(CheckTeam(player))
	times.Value += 1
end

end

function CheckTeam(plr)
local teams = teams:GetTeams()
for _, team in pairs(teams) do
local players = team:GetPlayers()
for _, player in pairs(players) do
if player == plr then
return team.Name
end
end
end
end

Players.PlayerAdded:Connect(leaderboardSetup)

how about that?

1 Like

Are you sure the player’s team is changing?

Yep, because i see on the leaderboard, the player’s team changes

can you try this again but making the whole thing in the code block like

this
local Players = game:GetService("Players")
local teams = game:GetService("Teams")
local tof = false
local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local times = Instance.new("IntValue")
	times.Name = "times"
	times.Value = 0
	times.Parent = leaderstats
	while true do
		task.wait(1)
		print(CheckTeam(player))
		times.Value += 1
	end
end

function CheckTeam(plr)
	local teams = teams:GetTeams()
	for _, team in pairs(teams) do
		local players = team:GetPlayers()
		for _, player in pairs(players) do
			if player == plr then
				return team.Name
			end
		end
	end
end

Players.PlayerAdded:Connect(leaderboardSetup)

how about that?
1 Like