I created ranktags in Roblox Studio, but the script only works for me until another person joins the game

Viewed 44

All the video's in youtube that I've watched are outdated and do not work anymore. The script that is not working:

local ServerStorage = game:GetService("ServerStorage")
local RankTag = ServerStorage.Ranktagtest
local connections = {}
local Players = game:GetService("Players")
local CloneTag = RankTag:Clone()
local NameTag = CloneTag.NameTag
local RankTag = CloneTag.RankTag
    
game.Players.PlayerAdded:Connect(function(player)
    connections[player] = player.CharacterAdded:Connect(function(character)
        CloneTag.Parent = character.Head
    
        NameTag.Text = player.Name
    
        if player:GetRankInGroup(0000) == 255 then
            RankTag.Text = "Grand Inquisitor"
            RankTag.TextColor3 = Color3.fromRGB(255,208,0)
        elseif player:GetRankInGroup(0000) == 254 then
            RankTag.Text = "Second Sister"
            RankTag.TextColor3 = Color3.fromRGB(255,0,0)
        elseif player:GetRankInGroup(0000) == 6 then
            RankTag.Text = "Third Sister"
            RankTag.TextColor3 = Color3.fromRGB(27,42,53)
        elseif player:GetRankInGroup(0000) == 5 then
            RankTag.Text = "Fourth Sister"
            RankTag.TextColor3 = Color3.fromRGB(27,42,53)
        elseif player:GetRankInGroup(0000) == 4 then
            RankTag.Text = "Eighth Brother"
            RankTag.TextColor3 = Color3.fromRGB(27,42,53)
        elseif player:GetRankInGroup(0000) == 3 then
            RankTag.Text = "Fifth Brother"
            RankTag.TextColor3 = Color3.fromRGB(27,42,53)
        elseif player:GetRankInGroup(0000) == 0 then
            RankTag.Text = "Civilian"
            character:WaitForChild("Humanoid").DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
        end
    end)
    
end)
    
   Players.PlayerRemoving:Connect(function(player)
    if connections[player] then
        connections[player]:Disconnect()
        connections[player] = nil
    end
end)

I am a learning coder and it would be much appreciated if someone could help. Yes I know there is a missing groupId, this is because this group is private. Thank you!

1 Answers

Assuming that RankTag works the way it is supposed to, my guess would be that the problem is that you are only cloning it once. Then every time a new player joins, they take the one tag and display it over their head.

The simple fix is to move these lines :

local CloneTag = RankTag:Clone()
local NameTag = CloneTag.NameTag
local RankTag = CloneTag.RankTag

Inside the PlayerAdded callback. That way, a new one will be cloned for each player that joins the game.

As a small note, player:GetRankInGroup(groupId) is an asynchronous fetch, and it can take some time for it to finish. You should try to limit the number of times you make this fetch and store the result.

Here's how I would structure the code...

local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local RankTag = ServerStorage.Ranktagtest
local connections = {}

-- define all ranks
local groupId = 0000
local unrankedId = 0
local ranks = {
    [255] = { "Grand Inquisitor", Color3.fromRGB(255,208,0) },
    [254] = { "Second Sister", Color3.fromRGB(255,0,0) },
    [6] = { "Third Sister", Color3.fromRGB(27,42,53) },
    [5] = { "Fourth Sister", Color3.fromRGB(27,42,53) },
    [4] = { "Eighth Brother", Color3.fromRGB(27,42,53) },
    [3] = { "Fifth Brother", Color3.fromRGB(27,42,53) },
    [unrankedId] = { "Civilian", Color3.fromRGB(255, 255, 255) },
}

    
game.Players.PlayerAdded:Connect(function(player)
    -- clone the name tag for the new player
    local CloneTag = RankTag:Clone()
    local NameTag = CloneTag.NameTag
    local RankTag = CloneTag.RankTag

    connections[player] = player.CharacterAdded:Connect(function(character)
        -- put the tag into the character model
        CloneTag.Parent = character.Head
        NameTag.Text = player.Name
    
        -- customize the tag based on group rank
        local playerRank = player:GetRankInGroup(groupId)
        local rankData = ranks[playerRank]
        if rankData then
            RankTag.Text = rankData[1]
            RankTag.TextColor3 = rankData[2]
        else
            warn ("No rank data defined for rank #" .. tostring(playerRank))
        end

        if playerRank == unrankedId then
            local humanoid = character:WaitForChild("Humanoid")
            humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
        end
    end)
end)

-- clean up when a player leaves
Players.PlayerRemoving:Connect(function(player)
    if connections[player] then
        connections[player]:Disconnect()
        connections[player] = nil
    end
end)
Related