Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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!

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

Answer this question