Assigning player ranks with teams

I am in the process of making an automatic load-out system and I have come across a problem in the way the script automatically assigns a load-out when a player joins a team, I know exactly what’s causing the issue I’m just not aware on how to solve it. As you can see I have a line of code that identifies a player’s team and if they are on a certain team they will be given a load-out, problem is the function that the code lies in is only fired when the player joins. I have tried separating the code and making it’s own function resulting in failure, if you are aware of any ways to fix this issue please respond with any help you can give.
image
Line 102 starts the auto assign function when you join the “Hostile” team, as you can see it’s inside of “CheckClassRank”
which is only fired when the player joins as you can see here: image
If you need more details in order to find a way to fix this issue please don’t be hesitant to ask.

EDIT: I want the lines between 102 and 105 fired when the player joins the “Hostile” team. Not just when the player joins.

1 Like

Here what your wanna do

Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Character)
        local Class,Rank = CheckClassRank(Player);
    end)
end)

This way is fired everytime a respawn happens

2 Likes

Maybe add a player.Changed function inside of playeradded to check if the team changes?

1 Like

I agree with both @VineyardVine and @Firefaul on this. Both approaches can work. @VineyardVine’s will give the correct load-out when the player respawns. @Firefaul’s will do so when the team changes. The caveat with that one though is that you need to make sure you clear whatever current load-out exists first.

Something like this:

game.Players.PlayerAdded:Connect(function(player)

	-- Initial check:
	local class, rank = checkClassRank(player)

	-- Team changed:
	player:GetPropertyChangedSignal("Team"):Connect(function()
		-- Recheck:
		class, rank = checkClassRank(player)
		-- Redo loadout if character is in-game:   (NOTE: Might be better to also check if Humanoid exists and has health > 0)
		if player.Character then
			clearLoadout(player)
			giveLoadout(player, class, rank)
		end
	end)

	-- Give loadout when character spawns
	player.CharacterAdded:Connect(function(character)
		giveLoadout(player, class, rank)
	end)

end)
2 Likes

This does work, but the load-out doesn’t seem to actually fire… It still says “no loadout” even though the person is on the correct team and the lines work.

EDIT: the script isn’t correctly assigning the player, it’s not fault on anybody’s programming on this thread, there is now a different error. As you can see here:


It isn’t recognizing the load-out, I will follow up shortly once I get the line of code that assigns.

Now keep in mind it DOES pass this function (if player.teamcolor… etc) it just isn’t assigning the load-out correctly…
image

This is what it normally looks like when you first join to have a load-out assigned: image

I don’t see any problems that would relate to the teamcolor load-out that’s probably because i’m not an excellent scripter, but it should be pretty obvious if there was one. I’d also like to point out this assign on team works completely fine when you FIRST join and you’re automatically assigned on the team, however not when you switch teams.

It seems like the “id” variable you’re using as the group id represents the first variable for the in pairs statement. The first variable for an in pairs statement will always be the current position in the table. For example, id might be 1 inside of that block of code, not the group id.

So how would I go about fixing this issue?

Replace “id” in plr:GetRankInGroup(id) with the actual group id instead.

It isn’t the problem with the GetRankInGroup it’s the problem with assigning the rank when the person joined the Hostile team.

This works at assigning the player IF they are in a group:
image

But I want the player to be assigned when they are on a team not in a group:
image

if not plr.Neutral and plr.Team == game.Teams.Hostiles then
     --stuff
end

Sometimes it’s better to check the team property than teamcolour. This might fix it, it might not.