I need help on my script

I’m trying to make it so you will be assigned into a team based on your group role also roast me Ik this script is so wrong sob


local Teams = game:GetService("Teams")
local players = game:GetService("Players")

local function playerAdded(player)
    
    local playerRank = player:GetRankInGroup(35296736)
    
    if playerRank <= 254 then
        player.Team = Teams:FindFirstChild("Staff")
    end
        
    if playerRank >= 215 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 195 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 155 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 142 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 123 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 103 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 86 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 69 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 65 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 62 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 31 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank <= 29 then
        player.Team = Teams:FindFirstChild("Staff")
    end
    
    if playerRank >= 18 then
        player.Team = Teams:FindFirstChild("Business")
    end
    
    if playerRank <= 13 then
        player.Team = Teams:FindFirstChild("Business")
    end
    
    if playerRank >= 12 then
        player.Team = Teams:FindFirstChild("Comfort")
    end
    
    if playerRank <= 9 then
        player.Team = Teams:FindFirstChild("Economy")
    end
        
end
    
players.PlayerAdded:Connect(playerAdded)

image

Your way of executing the conditional statements are arbitrarily separate with a lack of logical structure: They act as separate conditional statements as if none of them are mutually exclusive in any way. Use elseif statements for each new one after the first, since you can only be in one team.

Here’s how you can try to think this through:

  • Imagine if the user is rank 255, it will set the team to Staff on the second statement in the sequence.
  • It will repeat this until >= 18, which will result in the Business team instead.
  • The final result will be Comfort after the check at >= 12.
  • This does not add up if the rank is 255 and that they were given the team with the lowest corresponding rank.
  • The reverse is true if the rank is set to 1, the user is given Staff for more than one operation before the final team is Economy.

Also try avoiding mixing both >= and <= operators, because you’ll capture a set of everything if you do rank >= x and rank <= x. Consult set theory for further information. I’ll advise you to use >= operator though, as it is most logical.

2 Likes
  1. you can use GetRankInGroup instead of checking the role id
  2. GetRankInGroup() and GetRankInGroup can return an error if roblox is down so I suggest wrapping it in pcall to handle the exceptions
  3. I suggest you check this article Don't repeat yourself - Wikipedia

when a big portion of ur script is just you spamming if statements than you should consider better alternatives

I would also suggest considering learning about module scripts and table lookups and instance lookups

1 Like

you can use if statements for this but there are other ways
you are using if multiple times but… the player cannot have more than 1 rule so its a waste of resources because if an if condition was true it will still check for the other ifs which is useless you can use elseif instead and use the or, and keywords instead of making mutliple ifs like

if playerRank >= 31 or playerRank <= 29 then
        player.Team = Teams:FindFirstChild("Staff")
end

Another way will be using tables

local Teams = game:GetService("Teams")
local players = game:GetService("Players")

-- Will store the ranks 
local ranks = {
	[254]  = "Staff",
	[215] = "Staff",
	[18] = "Business",
	[9] = "Economy",
	-- etc....
}

local function playerAdded(player)
	local playerRank = player:GetRankInGroup(35296736)
	
	local playerGroupRole = ranks[playerRank]
	
	player.Team = Teams:FindFirstChild(playerGroupRole)
end

Using separate if statements instead of elseif caused overlapping conditions. and mixed usage of <= and >= created conflicting ranges.