Cant distribute items to certain teams. Any help?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. Im trying to achieve giving certain teams items

  3. What is the issue? Include screenshots / videos if possible!

  4. Really dont know. Wont give me any errors

  5. What solutions have you tried so far? Did you look for solutions on the Creator Hub?

  6. I looked online.

  7. creator hub.

  8. and a couple tut. (None of these works. I did write the code myself though)

local Players = game:GetService(“Players”)
local Teams = game:GetService(“Teams”)
local ServerStorage = game:GetService(“ServerStorage”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

local Keycard = ServerStorage:WaitForChild(“Keycard”, 5)
local Cuffs = ServerStorage:WaitForChild(“Cuffs”, 5)

local function isTeamAllowed(team)
for i, allowedTeam in ipairs(Teams) do
if team == allowedTeam then
return true
end
end
return false
end

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local backpack = player:FindFirstChild(“Backpack”)
if isTeamAllowed(player.Team) then

		if Keycard and not backpack:FindFirstChild("Keycard") then
			Keycard:Clone().Parent = backpack
			print("Gave " .. player.Name .. " Keycard")
		end


		if Cuffs and not backpack:FindFirstChild("Cuffs") then
			Cuffs:Clone().Parent = backpack
			print("Gave " .. player.Name .. " Cuffs")
		end
	end
end)

end)

Im not asking for an entire script. but please give me feedback on what I should do

1 Like

plus fomrat your code using the

`

3 times at the start and end of your script it makes it hard to read

Try changing

for i, allowedTeam in ipairs(Teams) do
...

to

for i, allowedTeam in Teams:GetChildren() do
...

Also why would you need this, you could remove this entire part to be honest.

you’d wanna do something like this:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local teamTools = {
	[Teams.Security] = {"Keycard", "Cuffs"},
	[Teams.Staff] = {"Keycard"}
}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local backpack = player:FindFirstChild("Backpack")
		if teamTools[player.Team] then
			for _, toolName in teamTools[player.Team] do
				local tool = ServerStorage:FindFirstChild(toolName)
				if tool then
					tool:Clone().Parent = backpack
				end
			end
		end
	end)
end)