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

How come my rank system does not work after a player changes teams?

So I'm currently working on a Rank system that is determined by the players team, and level but for some reason when It re-assigns the player's rank, when they level up it does not stick and continues to run off of their previous team's ranks. What is happening?

---//Services\\---
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//ServiceChildren\\--
local USRankHandler = require(ReplicatedStorage.RankHandler:WaitForChild("USRankHandler")) ---module script
local VRankHandler = require(ReplicatedStorage.RankHandler:WaitForChild("VRankHandler")) ---module script
---//USRankHandler\\---
game.Players.PlayerAdded:Connect(function(player)
	if player.TeamColor == BrickColor.new("Cyan") then
		player:WaitForChild("leaderstats").Rank.Value = USRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value) ---Rank is a StringValue level is a IntValue

		player.leaderstats2.Level:GetPropertyChangedSignal("Value"):Connect(function()
			player:WaitForChild("leaderstats").Rank.Value = USRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value)
		end)
	end
end)
--//USRankTeamChange\\--
game.Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
		if player.TeamColor == BrickColor.new("Cyan") then
			player:WaitForChild("leaderstats").Rank.Value = USRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value)
		end
	end)
end)

---//VRankHandler\\---
game.Players.PlayerAdded:Connect(function(player)
	if player.TeamColor == BrickColor.new("Bright red") then
		player:WaitForChild("leaderstats").Rank.Value = VRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value) ---Rank is a StringValue level is a IntValue

		player.leaderstats2.Level:GetPropertyChangedSignal("Value"):Connect(function()
			player:WaitForChild("leaderstats").Rank.Value = VRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value)
		end)
	end
end)
--//VRankTeamChange\\--
game.Players.PlayerAdded:Connect(function(player)
	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
		if player.TeamColor == BrickColor.new("Bright red") then
			player:WaitForChild("leaderstats").Rank.Value = VRankHandler.Rank(player:WaitForChild("leaderstats2").Level.Value)
		end
	end)
end)
--###----------[[VARIABLES]]----------###--
local USRanks = {

	"Private",
	"PFC",
	"Corporal",
	"Sergeant",
	"Staff Sergeant",
	"Technical Sergeant",
	"First Sergeant",
	"Master Sergeant",
	"Second Lieutenant",
	"First Lieutenant",
	"Captain",
	"Major",
	"Lieutenant Colonel",
	"Colonel",
	"Brigadier General",
	"Major General",
	"Lieutenant General",
	"General",
	"General of the Army";

}



--###----------[[SETUP]]----------###--
return {

	Rank = function(MemberRank)
		if (MemberRank > 20) then
			return MemberRank .. "?CINC?"
		elseif (MemberRank == 20) then
			return "?CINC?"
		end
		---------------
		return USRanks[MemberRank]
	end

}

--###----------[[VARIABLES]]----------###--
local VRanks = {

	"Binh nhì",
	"Binh nh?t",
	"H? s?",
	"Trung s?	",
	"Trung Snv.",
	"Trung Skt.",
	"Th??ng s?",
	"Thi?u úy",
	"Trung úy",
	"Th??ng úy",
	"??i úy",
	"Thi?u tá",
	"Trung tá",
	"Th??ng tá",
	"??i tá",
	"Thi?u t??ng",
	"Trung t??ng",
	"Th??ng t??ng",
	"??i t??ng";

}



--###----------[[SETUP]]----------###--
return {

	Rank = function(MemberRank)
		if (MemberRank >= 20) then
			return MemberRank .. "?CINC?"
		elseif (MemberRank == 20) then
			return "?CINC?"
		end
		---------------
		return VRanks[MemberRank]
	end

}

Thank you, Vincent

P.S If you need the module scripts please comment asking! :) Also sorry if the code is a mess.

8 comments

0

It is better if you just compare the Team Objects than their BrickColors. The dual use of 'PlayerAdded' is redundant, and can be additionally handled within the original Callback; you don't actually check for a difference either, both functions read for Cyan, which makes it again unnecessary.

0

Another thing to note is the fact that load-latency isn't a concern on the Server. The leaderstats folder isn't going anywhere, so you don't need to continuously yield for it. You also should be allocating the Instance if you're going to refer to it several times.

1

I am struggling to understand your description of the problem. Could you please provide an example of what's going wrong, as well as the ModuleScripts responsible for determining the Player's Rank.

0

I provided the module scrips.

0

From your first comment, when I got rid of the 'PlayerAdded' from where I thought you where talking about(TeamChange stuff) I noticed that it would not work at all I'm required to do 'PlayerAdded' or it just wont work.

0

And personally for me it's easier to use 'TeamColor'. As I just find it easier to use when changing teams.

0

I am willing to Contact you over discord.

0

And I have no Idea on how to use Team, but I tried to compare it and it did not work at all.

1 answer

✓ Accepted answer

The following code was reached through consultation on Discord.

--###----------[[SERVICES]]----------###--
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Players = game:GetService("Players")



--###----------[[VARIABLES]]----------###--
local RankHandlers = ReplicatedStorage.RankHandlers



--###----------[[FUNCTIONS]]----------###--
local function GetRespectiveRank(Player, Level)
    local RankHandler = RankHandlers:FindFirstChild(Player.Team.Name)
    if (RankHandler) then
        return require(RankHandler).Rank(Level)
    else
        warn("Couldn't find a RankHandler for the \"" .. Player.Team.Name .. "\" team.")
    end
    ---------------
    return "[N/A]"
end

local function ManageRank(Player)
    local Leaderstats = Player:WaitForChild("leaderstats")
    ---------------
    local Rank = Leaderstats.Rank
    local Level = Leaderstats.Extra.Level
    ---------------
    Level:GetPropertyChangedSignal("Value"):Connect(function()
        Rank.Value = GetRespectiveRank(Player, Level.Value)
    end)
    ---------------
    Player:GetPropertyChangedSignal("Team"):Connect(function()
        Rank.Value = GetRespectiveRank(Player, Level.Value)
    end)
    ---------------
    Rank.Value = GetRespectiveRank(Player, Level.Value)
end



--###----------[[SETUP]]----------###--
Players.PlayerAdded:Connect(ManageRank)

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