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
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.
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)
my game is r6 so humanoid root part wont work
R6 rigged characters have a HumanoidRootPart.
oh im getting this error “infinite yeild possibility wait for child humanoid root part”
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.
Forgot to push the edit button, now try, thanks.
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
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)
I had to change the script so that it checks the players TeamColor property not their Team property.
it works, but it gives u a lot of tools and doesnt remove them when u step off the part
how do i make it so only one tool spawns
also on my end it doesnt remove the tools
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)
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
I moved the tool to ReplicatedStorage in case you hadn’t noticed.
yeah i moved my tool to rs too