Trying to give a tool when a player touches a part for a specific team

Hi. I am trying to give a tool when a player touches a part for a specific team and when they are not touching the part they don’t have the tool anymore

1 Like

Every player has a team property, check if that is the desired one and then give the tool. To detect that they arent touching anymore i reccomend .TouchEnded but you can use other stuff i suppose, also add a debounce or something to not make it spammed. Good luck.

3 Likes
local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage:WaitForChild("Tool") --change to name of tool
local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local partPlayers = {}
local teamName = "" --change to team name

part.Touched:Connect(function(hit)
	if hit.Parent:WaitForChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player.Team == teamName then
			table.insert(partPlayers, player)
			local toolClone = tool:Clone()
			toolClone.Parent = player.Backpack
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:WaitForChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player.Team == teamName then
			for i, v in pairs(partPlayers) do
				if v.Name == player.Name then
					player:WaitForChild("Backpack"):FindFirstChild("Tool"):Destroy()
					table.remove(partPlayers, i)
				end
			end
		end
	end
end)
3 Likes

my game is r6 so humanoid root part wont work

1 Like

R6 rigged characters have a HumanoidRootPart.

2 Likes

oh im getting this error “infinite yeild possibility wait for child humanoid root part”

1 Like
local serverStorage = game:GetService("ServerStorage")
local tool = serverStorage:WaitForChild("Tool") --change to name of tool
local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local partPlayers = {}
local teamName = "" --change to team name

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player.Team == teamName then
			table.insert(partPlayers, player)
			local toolClone = tool:Clone()
			toolClone.Parent = player.Backpack
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player.Team == teamName then
			for i, v in pairs(partPlayers) do
				if v.Name == player.Name then
					player:WaitForChild("Backpack"):FindFirstChild("Tool"):Destroy() --change to name of tool
					table.remove(partPlayers, i)
				end
			end
		end
	end
end)

Minor mistake on my part, should have been “FindFirstChild” not “WaitForChild”. Thanks for notifying me on that. Also this is a server script in case you were weren’t sure.

1 Like

i put it in a server script beforehand and i got this error using the script u just posted

1 Like

Forgot to push the edit button, now try, thanks.

2 Likes

im not getting any errors but it isnt working

local serverStorage = game:GetService("ServerStorage")

local tool = serverStorage:WaitForChild("GK") --change to name of tool

local players = game:GetService("Players")

local part = workspace:WaitForChild("GoaliePart")

local partPlayers = {}

local teamName = "RedsGK" --change to team name
1 Like
local repStorage = game:GetService("ReplicatedStorage")
local tool = repStorage:WaitForChild("Tool") --change to name of tool
local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local partPlayers = {}

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print(player.Team)
		if player.TeamColor == BrickColor.new("White") then --change to color of team
			table.insert(partPlayers, player)
			local toolClone = tool:Clone()
			toolClone.Parent = player:WaitForChild("Backpack")
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print(player.Team)
		if player.TeamColor == BrickColor.new("White") then --change to color of team
			for i, v in pairs(partPlayers) do
				if v.Name == player.Name then
					player:WaitForChild("Backpack"):FindFirstChild("Tool"):Destroy() --change to name of tool
					table.remove(partPlayers, i)
				end
			end
		end
	end
end)
1 Like

I had to change the script so that it checks the players TeamColor property not their Team property.

1 Like

it works, but it gives u a lot of tools and doesnt remove them when u step off the part

1 Like

https://gyazo.com/1f3407893b7d95b6c680fcd8bfac514e

1 Like

how do i make it so only one tool spawns

1 Like

also on my end it doesnt remove the tools
image

1 Like
local repStorage = game:GetService("ReplicatedStorage")
local tool = repStorage:WaitForChild("Tool") --change to name of tool
local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local partPlayers = {}
local debounce1 = false
local debounce2 = false

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		print(player.Team)
		if player.TeamColor == BrickColor.new("White") then --change to color of team
			table.insert(partPlayers, player)
			local toolClone = tool:Clone()
			toolClone.Parent = player:WaitForChild("Backpack")
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player.TeamColor == BrickColor.new("White") then --change to color of team
			for i, v in pairs(partPlayers) do
				if v.Name == player.Name then
					table.remove(partPlayers, i)
					for i, v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
						if v.Name == "Tool" then --change to name of tool
							v:Destroy()
						end
					end
				end
			end
		end
	end
end)

https://gyazo.com/bd3f33a7561f22fd2b47719728c8986c

1 Like

it still doesnt destroy the tool or spawn in one tool, idk if there is smthing wrong on my end. my team colour is forest green, part name is GoaliePartReds and tool name is GK

1 Like

I moved the tool to ReplicatedStorage in case you hadn’t noticed.

1 Like

yeah i moved my tool to rs too

1 Like