Need help with giving a different gun to different teams

I am trying to give a certain gun to players on a team, while giving the players on the other team a different gun.
Here’s my code:

This is a local script and is only local to the player, you’ll need to use a server script. Localplayer also won’t work, so you’ll have to figure out something else.

local TeamBlue = game.Teams.Blue
local TeamBlueTools = {
	game.ServerStorage.Tools.Pistol,
}
local TeamRed = game.Teams.Blue
local TeamRedTools = {
	game.ServerStorage.Tools.Pistol2,
}



game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if Player.Team == TeamBlue then
			for i = 1, #TeamBlueTools do
				local Clone = TeamBlueTools[i]:Clone()
				Clone.Parent = Player.Backpack
			end
		elseif Player.Team == TeamRed
			for i = 1, #TeamRedTools do
				local Clone = TeamRedTools[i]:Clone()
				Clone.Parent = Player.Backpack
			end
		end
	end)
end)

Would this work?


I forgot to remove “Debounce”, it doesn’t do anything.

Here’s the updated script:

local TeamBlue = game.Teams.Blue
local TeamRed = game.Teams.Red
local RedTeamGunP = game.ReplicatedStorage.Tools.Pistol:Clone()
local BlueTeamGunP = game.ReplicatedStorage.Tools.PistoI:Clone()

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
if player.Team == TeamBlue then
RedTeamGunP.Parent = player.Backpack
else if player.Team == TeamRed then
BlueTeamGunP.Parent = player.Backpack
end
end
end)
end)

I’d recommend putting a wait for child for the teams.

local TeamBlue = game:GetService("Teams"):WaitForChild("Blue")
local TeamBlue = game:GetService("Teams"):WaitForChild("Red")
1 Like

Ok, I’ll try it out thanks.__.

local TeamBlue = game:GetService("Teams"):WaitForChild("Blue")
local TeamBlue = game:GetService("Teams"):WaitForChild("Red")
local RedTeamGunP = game.ReplicatedStorage.Tools.Pistol
local BlueTeamGunP = game.ReplicatedStorage.Tools.PistoI

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		if Player.Team == TeamBlue then
			BlueTeamGunP:Clone().Parent = Player.Backpack
		elseif Player.Team == TeamRed then
			RedTeamGunP:Clone().Parent = Player.Backpack
		end
	end)
end)

Here’s the final working code:

local TeamBlue = game:GetService(“Teams”):WaitForChild(“Blue”)
local TeamRed = game:GetService(“Teams”):WaitForChild(“Red”)
local RedTeamGunP = game.ReplicatedStorage.Tools.Pistol:Clone()
local BlueTeamGunP = game.ReplicatedStorage.Tools.PistoI:Clone()

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
if player.Team == TeamBlue then
RedTeamGunP.Parent = player.Backpack
else if player.Team == TeamRed then
BlueTeamGunP.Parent = player.Backpack
end
end
end)
end)

Thank you everyone who helped me.

1 Like

Hi, we’re here to help you - never expect us spoonfeed you, like give you the whole script. Like this, you don’t learn anything.

@kylerzong Instead of putting the whole script, link them some documentation and some ideas on how he can do it.