Adding specific players to a team on joining

I was attempting to use a script which adds specific players to a different team, which, for instance, allows them to access areas locked behind a team-only door, I do have a script which was supposed to do that, but it does not work properly.

The script which is supposed to move specific players to said team works incorrectly, the player list says that the player is in the team they were supposed to move to, but they are still in the basic team, so they cannot access team-only doors.

I have been attempting to find any help or working scripts on the Roblox Developer Forum, or scripting tutorials on YouTube, but it did not bring anything useful.

This is the code that I was using, it was supposed to be placed in StarterCharacterScripts, attempted to do so with normal scripts and local scripts.

player = game.Players.LocalPlayer
if player.Name == "Toxy4576" then -- the player name
	player.Team = game.Teams.Scientists -- the team name
end

Any way I can upgrade this script to work properly, or is there any existing script which I could use? I am thankful for all help!

2 Likes

Why not do in ServerScriptService a PlayerAdded event and check if the UserId (since usernames can be changed) of a person is within a table of UserIds (easier use) and then change the team?

3 Likes

Using an UserId would be definitely more convenient for this kinda script, but I have no idea how to program the script itself.

1 Like

Try this:

local devs = {
    -- Ids here
}

game.Players.PlayerAdded:Connect(function(plr)
    for _, dev in pairs(devs) do
        if plr.UserId == dev then
            plr.Team = game.Teams.Scientists
        end
    end
end)

Written on mobile

8 Likes

I will test this script out, should I place it in ServerScriptService or somewhere else?

1 Like

Usually where server scripts run, I assumed ServerScriptService is the best.

2 Likes

It appears to be working correctly, I am really thankful for the help!

2 Likes

Don’t forget to mark it as :white_check_mark: since it helped, so others can understand from this! :smiley:

3 Likes

I did mark the script as the solution, thank you, and I hope it will help someone else who will need the same script in the future.

2 Likes