Team Exclusive Items

In my game, there’s a team selection GUI which forces the player to join one of the two teams. I was wondering how I would be able to add team specific items that are given when the player joins one of the two teams.

Team Selection Server Script:

local replicatedStorage = game:GetService('ReplicatedStorage')
local players = game:GetService('Players')
local teams = game:GetService('Teams')

local immigrants = teams:WaitForChild('Immigrants')
local guards = teams:WaitForChild('Guards')
local choosingTeam = teams:WaitForChild('Choosing')

local chooseEvent = replicatedStorage.RemoteEvents.ChangeTeams

local function onEvent(playerFrom,teamChosen)
	print("recievcd")
	if teamChosen == immigrants then
		playerFrom.Team = immigrants
	elseif teamChosen == guards then
		playerFrom.Team = guards
	else
		playerFrom.Team = choosingTeam
		if playerFrom.Character then
			playerFrom.Character:Destroy()
		end
		return
	end
	playerFrom:LoadCharacter()
end


local function onPlayerJoined(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild('Humanoid')
		humanoid.Died:Once(function()
			task.wait(players.RespawnTime)
			if player.Character == character then
				player:LoadCharacter()
			end
		end)
	end)
end

chooseEvent.OnServerEvent:Connect(onEvent)
players.PlayerAdded:Connect(onPlayerJoined)

Local Script:

local replicatedStorage = game:GetService('ReplicatedStorage')
local players = game:GetService('Players')
local teams = game:GetService('Teams')

local immigrants = teams:WaitForChild('Immigrants')
local guards = teams:WaitForChild('Guards')
local choosingTeam = teams:WaitForChild('Choosing')

local chooseEvent = replicatedStorage.RemoteEvents.ChangeTeams
local player = players.LocalPlayer
local screenGui = replicatedStorage.GUI.TeamSelectMenu

screenGui.ResetOnSpawn = false
screenGui.Parent = player:WaitForChild("PlayerGui")

local function showPicker()
	if player.Team == choosingTeam then
		screenGui.Enabled = true
	else
		screenGui.Enabled = false
	end
end

showPicker()

player:GetPropertyChangedSignal('Team'):Connect(showPicker)

local immigrantButton = screenGui.ImmigrantFrame.TextButton
local GuardButton = screenGui.GuardFrame.TextButton

immigrantButton.MouseButton1Click:Connect(function()
	chooseEvent:FireServer(immigrants)
	print("fired")
end)

GuardButton.MouseButton1Click:Connect(function()
	chooseEvent:FireServer(guards)
	print("fired")
end)
1 Like

well I would personally make a items folder in serverstorage then make a folder for each team and store items in there.

yeah i did that but idk how to put them into the players backpack after they choose their team

1 Like

ah ok, here is a function you can use to add the tool into their backpack

local function GiveTool(plr, TeamName) -- you're gonna have to format this in studio
local TeamFolder = game.ServerStorage.Tools:FindFirstChild(TeamName)
local Char = plr.Character or plr.CharacterAdded:Wait()
if not Char then return "Character Didn't load in time" end
if not TeamFolder then return `{TeamName} Not Found In Server Storage.` end
for _, Tool in TeamFolder:GetChildren()
local ToolClone = Tool:Clone()
ToolClone.Parent = Char
-- add tool logic here if you want or you can write the logic in a local script inside of the tool.
end
end)

and then when you want to give it just add it to your onEvent function.

GiveTool(PlayerFrom, teamchosen) 

would i put this in the server script?

1 Like

Yes, you would because the client can’t replicate items to everyone nor can it access serverstorage, also I didn’t use replicatedstorage because exploiters can just replicate the item or use the code from the item on their item.

1 Like

let me know if that works for you!

2 things

1st: I put in the script but now I’m getting

argument #1 expects a string, but Instance was passed

function:

local function GiveTool(plr, TeamName) -- you're gonna have to format this in studio
	print("functioned")
	local TeamFolder = game:GetService("ServerStorage").Tools.TeamSpecificItems:FindFirstChild(TeamName)
	local Char = plr.Character or plr.CharacterAdded:Wait()
	if not Char then return "Character Didn't load in time" end
	if not TeamFolder then return `{TeamName} Not Found In Server Storage.` end
	for _, Tool in TeamFolder:GetChildren() do
		local ToolClone = Tool:Clone()
		ToolClone.Parent = Char
		-- add tool logic here if you want or you can write the logic in a local script inside of the tool.
	end
end


local function onEvent(playerFrom,teamChosen)
	print("recievcd")
	if teamChosen == immigrants then
		playerFrom.Team = immigrants
	elseif teamChosen == guards then
		playerFrom.Team = guards
		GiveTool(playerFrom, teamChosen) 
	else
		playerFrom.Team = choosingTeam
		if playerFrom.Character then
			playerFrom.Character:Destroy()
		end
		return
	end
	playerFrom:LoadCharacter()
end

2nd: the players character is destroyed after they select a team so that they can go to the designated spawn. Should I make the function wait before it goes into the players inventory or is there a better way?

edit: function is connecting but item is not going into inventory (checked with a print)

ah I see my misunderstanding, I parented the tool to the character meaning they hold it right after they choose their team. You should changed char to plr.Backpack also are you calling onEvent when the character is added or when the player is added?

the argument error would be caused by your team variables not being a string so do teamChosen.Name

oh also if you need help i’m willing to play test with you!

when a button is pressed on the players GUI to choose the team, the local script fires a remote event that the server script detects to run onEvent

ah ok so move the givetool to characteradded

where would i change this?

function rn:

local function GiveTool(player, TeamName) -- you're gonna have to format this in studio
	print("functioned")
	local TeamFolder = game:GetService("ServerStorage").Tools.TeamSpecificItems:FindFirstChild(TeamName)
	local Char = player.Character or player.CharacterAdded:Wait()
	if not Char then return "Character Didn't load in time" end
	if not TeamFolder then return `{TeamName} Not Found In Server Storage.` end
	for _, Tool in TeamFolder:GetChildren() do
		local ToolClone = Tool:Clone()
		ToolClone.Parent = player.Backpack
		-- add tool logic here if you want or you can write the logic in a local script inside of the tool.
	end
end

above the player added function

this?

	player.CharacterAdded:Connect(function(character)
		GiveTool(player, teamChosen) 
		local humanoid = character:WaitForChild('Humanoid')
		humanoid.Died:Once(function()
			task.wait(players.RespawnTime)
			if player.Character == character then
				player:LoadCharacter()
			end
		end)
	end)

sorry for confusion

nope the playeradded function not characteradded

sorry for the late reply I just got a warning for complimenting someone :confused:

got this in console:

ServerScriptService.ChangeTeams:43: attempt to index nil with 'Name'

i think its because teamChosen isnt defined yet

local function onPlayerJoined(player, teamChosen)
	teamChosen.Name = "Guards" --think this is it?
	player.CharacterAdded:Connect(function(character)
		GiveTool(player, teamChosen) 
		local humanoid = character:WaitForChild('Humanoid')
		humanoid.Died:Once(function()
			task.wait(players.RespawnTime)
			if player.Character == character then
				player:LoadCharacter()
			end
		end)
	end)
end

chooseEvent.OnServerEvent:Connect(onEvent)
players.PlayerAdded:Connect(onPlayerJoined)

its fine lol dw