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)
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)
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.
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?
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
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)
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)