Team changing script not working

I am trying to create a script for an obby that changes a players team depending on their stage value. I modified another script for team changing via leaderstats value but it is not working. I need the players team to change to the correct team for that value range both when they join the game and when their leaderstats value changes. (my scripting skills are extremely limited so it is probably a really basic problem but I cannot find any other solution for my specific issue.)

The script:

local Teams = game:GetService("Teams")

local Team1 = Instance.new("Team", Teams)
Team1.Name = "Section 1"
Team1.AutoAssignable = true
Team1.TeamColor = BrickColor.new("Crimson")

local Team2 = Instance.new("Team", Teams)
Team2.Name = "Section 2"
Team2.AutoAssignable = false
Team2.TeamColor = BrickColor.new("Bright green")

local Team3 = Instance.new("Team", Teams)
Team3.Name = "Section 3"
Team3.AutoAssignable = false
Team3.TeamColor = BrickColor.new("Bright yellow")

local Sec1Range = NumberRange.new(1, 11)
local Sec2Range = NumberRange.new(12, 22)
local Sec3Range = NumberRange.new(23, 23)

--Section 1--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function(newValue)
		if newValue <= Sec1Range.Max and newValue >= Sec1Range.Min and player.Team ~= Team1 then
			player.Team = Team1
		end
	end)
end)

--Section 2--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function(newValue)
		if newValue <= Sec2Range.Max and newValue >= Sec2Range.Min and player.Team ~= Team2 then
			player.Team = Team2
		end
	end)
end)

--Section 3--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function(newValue)
		if newValue <= Sec3Range.Max and newValue >= Sec3Range.Min and player.Team ~= Team3 then
			player.Team = Team3
		end
	end)
end)

I believe the issue is with newValue. I did some testing and it always gives nil as the value. Instead, use something like:

Stage:GetPropertyChangedSignal("Value"):Connect(function()
        local newValue = Stage.Value
		if newValue <= Sec2Range.Max and newValue >= Sec2Range.Min and player.Team ~= Team2 then
			player.Team = Team2
		end
	end)

so i tried it but it didnt work

(heres the whole revised script in case i did something else wrong)

local Teams = game:GetService("Teams")

local Team1 = Instance.new("Team", Teams)
Team1.Name = "Section 1"
Team1.AutoAssignable = true
Team1.TeamColor = BrickColor.new("Crimson")

local Team2 = Instance.new("Team", Teams)
Team2.Name = "Section 2"
Team2.AutoAssignable = false
Team2.TeamColor = BrickColor.new("Bright green")

local Team3 = Instance.new("Team", Teams)
Team3.Name = "Section 3"
Team3.AutoAssignable = false
Team3.TeamColor = BrickColor.new("Bright yellow")

local Sec1Range = NumberRange.new(1, 11)
local Sec2Range = NumberRange.new(12, 22)
local Sec3Range = NumberRange.new(23, 23)


--Section 1--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function()
		local newValue = Stage.Value
		if newValue <= Sec1Range.Max and newValue >= Sec1Range.Min and player.Team ~= Team1 then
			player.Team = Team1
		end
	end)
end)

--Section 2--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function()
		local newValue = Stage.Value
		if newValue <= Sec2Range.Max and newValue >= Sec2Range.Min and player.Team ~= Team2 then
			player.Team = Team2
		end
	end)
end)

--Section 3--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function()
		local newValue = Stage.Value
		if newValue <= Sec3Range.Max and newValue >= Sec3Range.Min and player.Team ~= Team3 then
			player.Team = Team3
		end
	end)
end)

I’m pretty sure you can’t define variables as Values, instead you’d need to do

local newValue = Stage

And then change all of the newValue lines to newValue.Value

If that doesn’t work, might wanna try debugging it with print statements instead?

So I tried it but it still didn’t work, I also combined all three functions bc it was a waste of space. Here’s my new script, any other suggestions?

local Teams = game:GetService("Teams")

local Team1 = Instance.new("Team", Teams)
Team1.Name = "Section 1"
Team1.AutoAssignable = false
Team1.TeamColor = BrickColor.new("Crimson")

local Team2 = Instance.new("Team", Teams)
Team2.Name = "Section 2"
Team2.AutoAssignable = false
Team2.TeamColor = BrickColor.new("Bright green")

local Team3 = Instance.new("Team", Teams)
Team3.Name = "Section 3"
Team3.AutoAssignable = false
Team3.TeamColor = BrickColor.new("Bright yellow")

--Section 1--
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Stage = leaderstats:WaitForChild("Stage")
	Stage:GetPropertyChangedSignal("Value"):connect(function()
		local newValue = Stage
		if newValue.Value <= 11 and newValue.Value >= 1 and player.Team ~= Team1 then
			player.Team = Team1
		end
		if newValue.Value <= 22 and newValue.Value >= 12 and player.Team ~= Team2 then
			player.Team = Team2
		end
		if newValue.Value <= 23 and newValue.Value >= 23 and player.Team ~= Team3 then
			player.Team = Team3
		end
	end)
end)


Hm, maybe consider adding a couple of print statements in-between

print("Test 1")
Stage:GetPropertyChangesSignal("Value"):connect(function() --connect is deprecated moment
print("Test 2")
local newValue = Stage

To see if that changes anything?

this is a really bad way of handling this, you’re making 3 playeradded connections with 3 connections to the same leaderstats value. Also you forgot to parent the teams to the “Teams” service, which is probably your issue with the original script. Here is the rewritten code I highly advise you to use:

local levels = {
	['Section 1'] = {
		BaseTeam = true,
		TeamColor = BrickColor.new('Crimson'),
		Range = {
			Min = 1,
			Max = 11,
		}
	},
	['Section 2'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright green'),
		Range = {
			Min = 12,
			Max = 22,
		}
	},
	['Section 3'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright yellow'),
		Range = {
			Min = 23,
			Max = 24,
		}
	}
}

local teams = {}

for name, info in pairs(levels) do
	local team = Instance.new('Team')
	team.Name = name
	team.AutoAssignable = info.BaseTeam
	team.TeamColor = info.TeamColor
	team.Parent = game:GetService('Teams')
	teams[name] = team
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local stage = leaderstats:WaitForChild("Stage")
	stage:GetPropertyChangedSignal('Value'):Connect(function(newValue)
		for name, info in pairs(levels) do
			if newValue >= info.Range.Min and newValue < info.Range.Max then
				player.Team = teams[name]
				break
			end
		end
	end)
end)

Please mark as solution if it fixed your issue!

still didn’t work but thanks anyway

Right I forgot. GetPropertyChangedSignal doesn’t pass the newvalue as an argument like .Changed does. Here’s the fixed script:

local levels = {
	['Section 1'] = {
		BaseTeam = true,
		TeamColor = BrickColor.new('Crimson'),
		Range = {
			Min = 1,
			Max = 11,
		}
	},
	['Section 2'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright green'),
		Range = {
			Min = 12,
			Max = 22,
		}
	},
	['Section 3'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright yellow'),
		Range = {
			Min = 23,
			Max = 24,
		}
	}
}

local teams = {}

for name, info in pairs(levels) do
	local team = Instance.new('Team')
	team.Name = name
	team.AutoAssignable = info.BaseTeam
	team.TeamColor = info.TeamColor
	team.Parent = game:GetService('Teams')
	teams[name] = team
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local stage = leaderstats:WaitForChild("Stage")
	stage:GetPropertyChangedSignal('Value'):Connect(function()
		for name, info in pairs(levels) do
			if stage.Value >= info.Range.Min and stage.Value < info.Range.Max then
				player.Team = teams[name]
				break
			end
		end
	end)
end)

still doesn’t work. the script is in the server script service, that’s where it should be right?

I tested it myself in a baseplate and it works fine. I believe there is something wrong with whatever script is setting the stage value
https://streamable.com/fl0beu

Ok I’ll look into that. I’m setting the script as the solution anyway. Thanks a lot!

The code suggested above used the GetPropertyChangedSignal in addition with PlayerAdded which doesn’t work because PlayerAdded will only execute once. This is the code but I fixed it by removing the GetPropertyChangedSignal argument.

local levels = {
	['Section 1'] = {
		BaseTeam = true,
		TeamColor = BrickColor.new('Crimson'),
		Range = {
			Min = 1,
			Max = 11,
		}
	},
	['Section 2'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright green'),
		Range = {
			Min = 12,
			Max = 22,
		}
	},
	['Section 3'] = {
		BaseTeam = false,
		TeamColor = BrickColor.new('Bright yellow'),
		Range = {
			Min = 23,
			Max = 24,
		}
	}
}

local teams = {}

for name, info in pairs(levels) do
	local team = Instance.new('Team')
	team.Name = name
	team.AutoAssignable = info.BaseTeam
	team.TeamColor = info.TeamColor
	team.Parent = game:GetService('Teams')
	teams[name] = team
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local stage = leaderstats:WaitForChild("Stage")
	for name, info in pairs(levels) do
		if stage.Value >= info.Range.Min and stage.Value <= info.Range.Max then
			player.Team = teams[name]
			break
		end
	end
end)

You actually can define a variable as a value.

Example:

local TestVariable = Test.Value

print(TestVariable)

In this instance, this variable would get the value of “Test”, then print it!