If a player activates / fires a tool then team into a team script

So like my previous posts i stated that my scriptings skills are very low, so this time i tried developing my own scripts that my achievement is getting to a plyers whens uses a tool it teams onto another team.

Mu issue is this i can’t develop so well and i don’t understant that much about scripting.

I have tried looking up about functions and teams from roblox official website.

local Team = BrickColor.new("Really red")

local Tool = script.Parent

local plr = game:GetService("Players")

local Teams = game:GetService("Teams")

Tool.Activated:Connect(function()
	print("Tool was Fired!")
	
	local plr = game:GetService("Players")
	if  plr == Teams then
		 Team = BrickColor.new("Really red")
	end 
	
	end)

I would be fine if someone could tell me what to use or what in im wrong…

inside of a player you can insert only local scripts but if you change the team of a player with a local script
the others players will not see it so inside of the player you need a local script that will fire a server in replicated storage if the tool was activated then make a normal script that will activate if the server run
and the norrmal script will change the team of the player ! :sweat_smile:

:confused: thats complicated to me well i have to figure it out!

Ok firstly,
Create a RemoteEvent inside the tool, name it ‘TeamChange’.
Secondly, create a LocalScript and a Server Script inside the tool.

Paste the following into the LocalScript

local tool = script.Parent

local remoteEvent = tool:WaitForChild('TeamChange')

tool.Activated:Connect(function()
    remoteEvent:FireServer()
end)

Then Paste the following into the Server Script;

local tool = script.Parent
local remoteEvent = tool:WaitForChild('TeamChange')

local settings = {
    teamName = 'putNameOfteamHere',
}

remoteEvent.OnServerEvent:Connect(function(player)
    player.Team = game.Teams[settings.teamName]
end)

Does this work with multiple tools?

yes of course just change the names

Yes, it should work atleast, if not then I dont know what to tell you

Yeah, it’ll work fine under any tool.

is it possible after the player dies it teams back to another team?

Sure, make a Server Script and put it in serverscriptservice, then paste the following and change the team variable;

local team = 'putNameOfTeamHere'

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1)
        character:WaitForChild('Humanoid').Died:Connect(function()
            player.Team = game.Teams[team]
        end)
    end)
end)

Does this aplpy to the scripts above, Like i want that player when he gets into that team, after he dies he teams back to the another team?

the script i sent on its own, will team a player to a certain team upon death, is that what’re you are asking?

Yeah somewhat like that. But first let me explain like what i want because it will be confusing. So the script i wanted is for a RP game so if a Citizen player fires / uses a gun he is teamed into the “enemy” team, and what i asked you after, was after the “enemy” player dies, He is teamed back to citizen (New life rule thingy to make the “police” team less confused) so your latest script is for like everyone so in terms of rp, if a policeman dies he is teamed into the citizen team. (i hope you understand this)

local team = 'Citizen'

local exceptions = { -- put the name of any team that you dont want teamed back when dead
      'Police',
      'AnotherTeam',
}

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1)
        character:WaitForChild('Humanoid').Died:Connect(function()
            if table.find(exceptions, player.Team.Name) then return end
            player.Team = game.Teams[team]
        end)
    end)
end)

Seems to work, but like when i get shooted and die it doesn’t team me onto the another team. like die from a weapon or stabbed. Only works when i reset manually.