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

Leaderstats not changing when player changes team. Any Ideas?

Hi, in my script i have a bit where if your in a certain team you get a divisional rank. But if you leave the team or join a new one where you have a different divisional rank it does not change. Any ideas?

local rankTbl = {
    -- format userid  = rank 
    [26331361] = "Overseer",
    [26332121] = "O5-2",
    [117372267] = "Site Director",
    [84185807] = "O5-X",
    [311774526] = "Class-D",
    [473500261] = "Level-1"
}
    local SD = BrickColor.new("Medium stone grey")
    local ET = BrickColor.new("Cool yellow")
    local MD = BrickColor.new("Bright bluish green")
    local CD = BrickColor.new("Bright orange")
    local FP = BrickColor.new("White")
    local CI = BrickColor.new("Really black")
    local CE = BrickColor.new("Black")
    local DEA = BrickColor.new("Pastel light blue")
    local DoR = BrickColor.new("Mulberry")
    local EC = BrickColor.new("Grime")
    local IA = BrickColor.new("Crimson")
    local ISD = BrickColor.new("Dusty Rose")
    local MAD = BrickColor.new("Deep orange")
    local MTF = BrickColor.new("Storm blue")
    local ScD = BrickColor.new("Deep blue")
game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"

    local rank = Instance.new("StringValue")
    rank.Name = "Rank"
    rank.Value = rankTbl[plr.UserId]  or "N/A" -- if rankTbl[plr.UserId] is nil pass 'N/A'
    rank.Parent = stats -- safe to do since stats has a nil parent ie not ingame

    local drank = Instance.new("StringValue")
    drank.Name = "Divisional Rank"
    drank.Value = "N/A"
    drank.Parent = stats

    -- When a player join
    if plr.Name == "tictac67" then -- Check player's name
    plr.TeamColor = SD
    elseif
        plr.Name == "tolly67p" then -- Check player's name
    plr.TeamColor = ET
    elseif
        plr.Name == "MINECRAFT_LOLBUILDZ" then
    plr.TeamColor = MD
    elseif
        plr.Name == "JackPlaysRBYT" then
    plr.TeamColor = CD
    elseif
        plr.Name == "consentlaw" then
    plr.TeamColor = FP


end

	 if plr.UserId == 26331361 then
        drank.Value = "Overseer"
    elseif plr.UserId == 26332121 and plr.Team.Name == "Engineering and Technical" then
        drank.Value = "Director"
    end

   

-- Refresh stop here --

    -- set stats parent after setup is complete
    stats.Parent = plr
end)


1 comment

0

The first one is always meant to be 'Overseer'

1 answer

✓ Accepted answer

This is because you are only creating the leaderstats only when the player joins the game. When does the PlayerAdded event fire for a single player during his/her session of playing the game: once (because he/she is only added once per session, which makes sense).

What you need to be checking for is a change in Team. There is a Team property of the Player object, and combining this knowledge with the use of GetPropertyChangedSignal, we can execute some code whenever a Player's Team is changed. Here's how it works:

player:GetPropertyChangedSignal("Team"):Connect(function()
	-- player's team has changed
end)

Given that, you should be able to incorporate it to meet your needs.

1 comment

0

'player' is undefined

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