Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can anyone help me debug this team tools script? It is not working and I have no idea why.

This is supposed to be a team tools script but the script is not working, I do not know why and I need some help.

local teamtools = game:GetService("ServerStorage"):WaitForChild("TeamTools"):GetChildren()
local teams = game:GetService("Teams").TeamStorage:GetChildren()

game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()
			wait(5)
			for i,v in pairs(teamtools) do
				local team = teams:FindFirstChild(v.Name)
				if team then
					if player.TeamColor == team.TeamColor then
						for u,o in pairs(v) do
							local newtool = o:Clone()
							newtool.Parent = player.Backpack
					end
				end
			end
		end
	end)
end)

1 answer

✓ Accepted answer

GENERAL PRACTICE

Use :GetService() to retrieve the Players service rather than game.Players

Use :WaitForChild() to make sure a part exists before changing / using it.

You can place multiple conditions in the same if-then statement using and or or

ISSUES

The "teams" variable is a table, so you cannot use :FindFirstChild() on it. Instead, remove the :GetChildren() portion of the variable and you will be retrieving the folder only.

REVISED SERVER SCRIPT

game:GetService("Players").PlayerAdded:Connect(function(player)
	local teamtools = game:GetService("ServerStorage"):WaitForChild("TeamTools"):GetChildren()
	local teams = game:GetService("Teams"):WaitForChild("TeamStorage")
	player.CharacterAdded:Connect(function(character)
		wait(5)
		for i, v in pairs(teamtools) do
			local team = teams:FindFirstChild(v.Name)
			if team and player.TeamColor == team.TeamColor then
				for u, o in pairs(v:GetChildren()) do
					local newtool = o:Clone()
					newtool.Parent = player:WaitForChild("Backpack")
				end
			end
		end
	end)
end)

Example for Set-Up: https://gyazo.com/18b1f581c1719764d01c8c6e41d4ef4b

This discussion is preserved as it was. New answers, comments, and votes are closed.