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

How do I remove and give a gear automatically when you join a team?

I'm trying to get the player get all their past tools removed and given new ones instantly after they switch teams, I don't know where to start with writing this script so any help is appreciated!

2 answers

✓ Accepted answer

Step 1.) Place tools inside Teams

Example: https://gyazo.com/910e954de4808da0f2bf1ff7e6987f75

Step2.) Place a script into ServerScriptService

paste this code into it.

function teamFromColor(color) 
for _,t in pairs(game:GetService("Teams"):GetChildren()) do 
if t.TeamColor==color then return t end 
end 
return nil 
end 

function onSpawned(plr) 
local tools = teamFromColor(plr.TeamColor):GetChildren() 
for _,c in pairs(tools) do 
c:Clone().Parent = plr.Backpack 
end 
end 

function onChanged(prop,plr) 
if prop=="Character" then 
onSpawned(plr) 
end 
end 

function onAdded(plr) 
plr.Changed:connect(function(prop) 
onChanged(prop,plr) 
end) 
end 

game.Players.PlayerAdded:connect(onAdded)

I hope this helped!

Create a Folder Inside of ServerStorage, Call it "Tools".

Create a Folder for every team inside of the first, name each the team's name and put the team's tools inside it.

Put this script in ServerScriptService:

local ServerStorage = game:GetService("ServerStorage")
local Teams = game:GetService("Teams")
local Tools = ServerStorage:FindFirstChild("Tools")

function ClearTools(Backpack,StarterGear)
	for i,v in ipairs(Backpack:GetChildren()) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end
	for i,v in ipairs(StarterGear:GetChildren()) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end
end

function TeamPlayerAdded(Player,Team)
	local Backpack = Player:WaitForChild("Backpack",3)
	local StarterGear = Player:WaitForChild("StarterGear",3)
	if Backpack and StarterGear then
		ClearTools(Backpack,StarterGear)
		local TeamTools = Tools:FindFirstChild(Team.Name)
		if TeamTools then
			for i,v in ipairs(TeamTools:GetChildren()) do
				if v:IsA("Tool") then
					v:Clone().Parent = Backpack
					v:Clone().Parent = StarterGear
				end
			end
		end
	end
end

for i,v in ipairs(Teams:GetTeams()) do
	v.PlayerAdded:Connect(function(Player)
		TeamPlayerAdded(Player,v)
	end)
end

Tools reset each time player changes teams

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