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)
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)
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)
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
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)