Team tool giver script isn't working

This script is supposed to give a tool when a player is on a certain team. I am not a scripter so I had to watch a video on how to do this. In the video, it worked for them but not me. I am pretty certain that I wrote it correctly as I do not see any mistakes nor do I see any errors in my console.

Script: (This is a ServerScript located in ServerScriptService, and the tools are parented to teams

function teamFromColor(color)
	for _, teams in pairs(game:GetService("Teams"):GetChildren()) do
		if teams.TeamColor == color then return teams end
	end
	return nil
end

function Spawned(player)
	local tools = teamFromColor(player.TeamColor):GetChildren()
	for _, tools in pairs(tools) do
		tools:Clone().Parent = player.Backpack
	end
end

function Adjusted(character, player)
	if character == "Character" then
		Spawned(player)
	end
end

function Joined(player)
	player.Changed:Connect(function(character)
		Adjusted(character, player)
	end)
end

game.Players.PlayerAdded:Connect(Joined)

Are the tools a child of the team like seen in the screenshot? You said so in your post but I just want to double check it looks like this.
dd

Yes, my sword is located in the team:
image

Edit:
Here is a video of it not working: https://streamable.com/63xjmt

function teamFromColor(color)
	for _, teams in pairs(game:GetService("Teams"):GetChildren()) do
		if teams.TeamColor == color then return teams end
	end
	return nil
end


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		plr:GetPropertyChangedSignal("Team"):Connect(function()
			local toolsToFind = teamFromColor(plr.TeamColor):GetChildren()
			for _, tools in pairs(toolsToFind) do
				tools:Clone().Parent = plr.Backpack
			end
		end)
	end)
end)

Give this a try, let me know if it works out for you. :slight_smile:

It might also be useful to add a check in there to see if the player isn’t on a team to prevent it returning nil.

That works but when I go back to the lobby team I still have the sword

Gotcha, if the player doesn’t die and just gets brought back to a different team it is probably useful to add in something that deletes previous tools whenever the team is changed like this (ignore my horrible formatting here)

function teamFromColor(color)
	for _, teams in pairs(game:GetService("Teams"):GetChildren()) do
		if teams.TeamColor == color then return teams end
	end
	return nil
end


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		plr:GetPropertyChangedSignal("Team"):Connect(function()
			
			for _, v in pairs(plr.Backpack:GetChildren()) do
				v:Destroy()
			end
			
			local toolsToFind = teamFromColor(plr.TeamColor):GetChildren()
			for _, tools in pairs(toolsToFind) do
				tools:Clone().Parent = plr.Backpack
			end
		end)
	end)
end)


1 Like

No offense to the tutorial’s creator but that script has a few glaring issues.

local teams = game:GetService("Teams")

for _, team in ipairs(teams:GetTeams()) do
	team.PlayerAdded:Connect(function(player)
		task.wait(1)
		local backpack = player:FindFirstChildOfClass("Backpack")
		if not backpack then return end
		for _, tool in ipairs(team:GetChildren()) do
			if tool:IsA("Tool") then
				local toolClone = tool:Clone()
				toolClone.Parent = backpack
			end
		end
	end)

	team.PlayerRemoved:Connect(function(player)
		local character = player.Character
		if not character then return end
		local humanoid = character:FindFirstChildOfClass("Humanoid")
		if not humanoid then return end
		if humanoid.Health <= 0 then return end
		local backpack = player:FindFirstChildOfClass("Backpack")
		if not backpack then return end
		humanoid:UnequipTools()
		for _, tool in ipairs(backpack:GetChildren()) do
			if tool:IsA("Tool") then
				tool:Destroy()
			end
		end
	end)
end
1 Like
plr:GetPropertyChangedSignal("Team"):Connect(function()

This connection shouldn’t be made inside a CharacterAdded event, this is going to lead to memory leaks down the road.

for _, v in pairs(plr.Backpack:GetChildren()) do
	v:Destroy()
end

This will only clear the backpack’s tools, what if the character has a tool equipped?

function teamFromColor(color)
	for _, teams in pairs(game:GetService("Teams"):GetChildren()) do
		if teams.TeamColor == color then return teams end
	end
	return nil
end

I’m not sure why this function exists when you can just index the player’s “Team” property via player.Team.

1 Like

Yeah, all of what was said here is correct, I did this pretty quickly in a rush and just edited the code he provided which had the original function in it.

I’d recommend making some changes to my original script in terms of optimization, as well as removing the tools in the character rather than just the backpack if you plan on having this as a released project.

Doing this script breaks all of my other scripts, I don’t know why but imma keep it the original fixed script as it 90% works + it doesn’t break the rest of my scripts

Edit: I see that if you have the sword equipped it does not remove it, still is a pretty big issue but I cannot fix this as if I do the rest of the scripts break

Had a minor spelling mistake (would have caused the script to error), should be working fine now.

1 Like

Everything seems to be working now, thank you very much :smiley: