How do I make a tool replacer if player already has the tools assigned to their current team it gets replaced by their switched team's tools

I don’t know how to make a tool replacer for my script that where if a player switches to team red(for example) and gets a sword, then switches to blue and gets a gun. I want the script to override(replace) the gun with the sword. Vice-versa the sword replaces the gun.

Here is my own.

– Put this script in ServerScriptService

– Agressive Hero –
local agressiveHeroTools = {
game.ServerStorage.AgressiveHeroSkillTools.FistOverdrive:Clone(),
game.ServerStorage.AgressiveHeroSkillTools.FistFury:Clone(),
game.ServerStorage.AgressiveHeroSkillTools.FinalTremor:Clone(),
game.ServerStorage.AgressiveHeroSkillTools.AngryPunch:Clone()
}

– Finalised Flames –
local finalisedFlamesTools = {
game.ServerStorage.FinalisedFlamesSkillTools.FireSplatter:Clone(),
game.ServerStorage.FinalisedFlamesSkillTools.HeatWave:Clone(),
game.ServerStorage.FinalisedFlamesSkillTools.HotandFast:Clone(),
game.ServerStorage.FinalisedFlamesSkillTools.BurstingAnger:Clone()
}

– Cold Rain –
local coldRainTools = {
game.ServerStorage.ColdRainSkillTools.HeavyWater:Clone(),
game.ServerStorage.ColdRainSkillTools.SplatteringKill:Clone(),
game.ServerStorage.ColdRainSkillTools.WaterOverload:Clone(),
game.ServerStorage.ColdRainSkillTools.WaterOverdrive:Clone()
}

local function addTeamTools(player, teamName)
if teamName == “Agressivehero” then
for _, tool in ipairs(agressiveHeroTools) do
tool:Clone().Parent = player.Backpack
end
elseif teamName == “FinalisedFlames” then
for _, tool in ipairs(finalisedFlamesTools) do
tool:Clone().Parent = player.Backpack
end
elseif teamName == “ColdRain” then
for _, tool in ipairs(coldRainTools) do
tool:Clone().Parent = player.Backpack
end
end
end

local function removePlayerTools(player)
for _, tool in ipairs(player.Backpack:GetChildren()) do
tool:Destroy()
end
end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
wait(5)
removePlayerTools(player)
end)
end)

player.CharacterAppearanceLoaded:Connect(function(character)
	local teamName = player.Team.Name
	removePlayerTools(player)
	addTeamTools(player, teamName)
end)

end)

game.Players.PlayerRemoving:Connect(function(player)
removePlayerTools(player)
end

(I dont know why player.CharacterAppearanceLoaded is turned into a code block even though it doesnt have backticks.)

Another one is this.

local animationId = “rbxassetid://123456789”
local damageAmount = 10
local tool = script.Parent
– Connect to the MouseClick event for the tool
tool.Activated:Connect(function()
– Get the player’s character and humanoid
local character = tool.Parent
local humanoid = character:FindFirstChildOfClass(“Humanoid”)

-- Play the animation
local animation = Instance.new("Animation")
animation.AnimationId = animationId
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()

-- Connect to the KeyframeReached event to detect when the animation hits another player
animationTrack.KeyframeReached:Connect(function(keyframeName)
	if keyframeName == "HitPlayer" then
		-- Detect if the animation hit another player and deal damage if it did
		local hitPart = animationTrack.Animation.AnimationData:GetMarkerReachedSignal("HitPlayer"):Wait()
		local hitCharacter = hitPart.Parent
		local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
		if hitHumanoid and hitHumanoid.Health > 0 and hitCharacter ~= character then
			hitHumanoid:TakeDamage(damageAmount)
		end
	end
end)

end)

I don’t know why it doesnt do damage too though.

(Again I don’t know why 80% of the script turn into a code block.)

Please format this code into a code block, it shows you how to in the devforum’s tutorial. People are choosing not to help, not because it’s hard or no one’s seeing it. But here’s a quick script that may help, and type this in console before using, and place the tools for each team in the team’s folder. This is also made assuming you have nothing in StarterPack
LoadTools.lua (473 Bytes)

local teamFolder = Instance.new('Folder'); teamFolder.Name = "Teams"; teamFolder.Parent = game.ServerStorage; for _,v in pairs(game.Teams:GetChildren()) do local new = Instance.new('Folder'); new.Parent = teamFolder; new.Name = v.Name end```

Hey, thanks for telling me, I don’t really use this forum much. Thanks for also giving a feedback too.

Did I add this right?

teamFolder.Name = "Teams"
teamFolder.Parent = game.ServerStorage
for _,v in pairs(game.Teams:GetChildren()) do
local new = Instance.new('Folder')
new.Parent = teamFolder
new.Name = v.Name
end

--Agressive Hero--
local tool1 = game.ServerStorage.AgressiveHeroSkillTools.FistOverdrive:Clone() -- Replace Tool1 with the name of your first tool
local tool2 = game.ServerStorage.AgressiveHeroSkillTools.FistFury:Clone() -- Replace Tool2 with the name of your second tool
local tool3 = game.ServerStorage.AgressiveHeroSkillTools.FinalTremor:Clone() -- Replace Tool3 with the name of your third tool
local tool4 = game.ServerStorage.AgressiveHeroSkillTools.AngryPunch:Clone() -- Replace Tool4 with the name of your fourth tool

--Finalised Flames--
local tool5 = game.ServerStorage.FinalisedFlamesSkillTools.FireSplatter:Clone()
local tool6 = game.ServerStorage.FinalisedFlamesSkillTools.HeatWave:Clone()
local tool7 = game.ServerStorage.FinalisedFlamesSkillTools.HotandFast:Clone()
local tool8 = game.ServerStorage.FinalisedFlamesSkillTools.BurstingAnger:Clone()

--Cold Rain--
local tool10 = game.ServerStorage.ColdRainSkillTools.HeavyWater:Clone() -- add .Clone() to the end of the line to create a new instance of the tool
local tool11 = game.ServerStorage.ColdRainSkillTools.SplatteringKill:Clone()
local tool12 = game.ServerStorage.ColdRainSkillTools.WaterOverload:Clone()
local tool13 = game.ServerStorage.ColdRainSkillTools.WaterOverdrive:Clone()

game.Teams.Agressivehero.PlayerAdded:Connect(function(player)
if player.Team == game.Teams.Agressivehero then
tool1:Clone().Parent = player.Backpack
tool2:Clone().Parent = player.Backpack
tool3:Clone().Parent = player.Backpack
tool4:Clone().Parent = player.Backpack
else
warn("Stack end on line 24")
end
end)

game.Teams.FinalisedFlames.PlayerAdded:Connect(function(player)
if player.Team == game.Teams.FinalisedFlames then
tool5:Clone().Parent = player.Backpack
tool6:Clone().Parent = player.Backpack
tool7:Clone().Parent = player.Backpack
tool8:Clone().Parent = player.Backpack
else
warn("Stack end on line 35")
end
end)

game.Teams.ColdRain.PlayerAdded:Connect(function(player) -- change FinalisedFlames to ColdRain
if player.Team == game.Teams.ColdRain then -- change FinalisedFlames to ColdRain
tool10:Clone().Parent = player.Backpack
tool11:Clone().Parent = player.Backpack
tool12:Clone().Parent = player.Backpack
tool13:Clone().Parent = player.Backpack
else
warn("Stack end on line 46")
end
end)

And did I script this right for it to deal damage?

local damageAmount = 10
local tool = script.Parent
-- Connect to the MouseClick event for the tool
tool.Activated:Connect(function()
	-- Get the player's character and humanoid
	local character = tool.Parent
	local humanoid = character:FindFirstChildOfClass("Humanoid")

	-- Play the animation
	local animation = Instance.new("Animation")
	animation.AnimationId = animationId
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()

	-- Connect to the KeyframeReached event to detect when the animation hits another player
	animationTrack.KeyframeReached:Connect(function(keyframeName)
		if keyframeName == "HitPlayer" then
			-- Detect if the animation hit another player and deal damage if it did
			local hitPart = animationTrack.Animation.AnimationData:GetMarkerReachedSignal("HitPlayer"):Wait()
			print("Hit part:", hitPart.Name)
			local hitCharacter = hitPart.Parent
			local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
			if hitHumanoid and hitHumanoid.Health > 0 and hitCharacter ~= character then
				hitHumanoid:TakeDamage(damageAmount)
			end
		end
	end)
end)

Here’s also my folder for the team tools.

The script I sent before already does that, but in an easier form so you can just put whatever tool u want for whatever team in the folder and it’ll do it automatically. Oh and I don’t see any problems with the damage script.