Basically, I have this Roles GUI which gives a player a tool and removes the others. I’ve been experimenting with code such as this
for _,v in pairs(player:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
but it doesn’t work. If I’m honest, this isn’t really my area of coding hence I’m not good at it. Can anyone help me please?
TL;DR: A script to remove certain tools IF the player has it.
local toolstoremove = {
"DispatchBatton",
"Whistle",
"AvantixMobile"
}
local backpack = player.Backpack
for i=1,#toolstoremove do
local tool = backpack:FindFirstChild(toolstoremove[i])
if tool then
tool:Destroy()
end
end
Where are you defining the variables AvantixMobileFind, DispatchBatonFind and WhistleFind? It doesn’t seem that you’re defining these from within your loop, which means it’s probably not looking within the player’s backpack in order to find the tools.
Your script is also looking through the player rather than players. If you take a typical player’s descendants, you’d be given “Backpack”, “PlayerGui” and “PlayerScripts”. Your code essentially looks in the Backpack for another backpack before it then gets the tool and then destroys it.
local Players = game:GetService("Players")
local toolsToRemove = {"DispatchBatton","Whistle","AvantixMobile"}
for _, player in pairs(Players:GetPlayers()) do
-- puts any tools they have equipped back into backpack
if Player.Character then
if Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid:UnequipTools()
end
end
local backpack = player:FindFirstChild("Backpack")
if backpack then
for i, toolName in pairs(toolsToRemove) do
if backpack:FindFirstChild(toolName) then
backpack:FindFirstChild(toolName):Destroy()
end
end
end
end
Alright - does the above script work for you? If you want it so different tools are removed depending on teams (You said it’s a role GUI), then you can make it so that the table is like this
for _,v in pairs(player:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
-- Just repeat with player.backpack
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
Here is the full script though if anyone is interested:
local DispatchRole = false
local DriverRole = false
local GuardRole = false
local NoRole = true
local ServerStorage = game:GetService("ServerStorage")
local DispatchRoleGUI = ServerStorage.OverheadRolesGUI.DispatchRole
local GuardRoleGUI = ServerStorage.OverheadRolesGUI.GuardRole
local DriverRoleGUI = ServerStorage.OverheadRolesGUI.DriverRole
local Whistle = ServerStorage.Tools.Whistle
local AvantixMobile = ServerStorage.Tools.AvantixMobile
local DispatchBaton = ServerStorage.Tools.DispatchBaton
local debounce = false
game.Workspace.OverheadGUIs.RemoteEvent.OnServerEvent:connect(function(player, cmd)
local WhistleFind = player.Backpack:FindFirstChild("Whistle")
local AvantixMobileFind = player.Backpack:FindFirstChild("AvantixMobile")
local DispatchBatonFind = player.Backpack:FindFirstChild("DispatchBaton")
local backpack = player.Backpack
if cmd == "DispatcherRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
DispatchRole = true
GuardRole = false
DriverRole = false
NoRole = false
if not debounce then
debounce = true
local clonedgui = DispatchRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
local clonedwhistle = Whistle:Clone()
clonedwhistle.Parent = player.Backpack
local clonedbaton = DispatchBaton:Clone()
clonedbaton.Parent = player.Backpack
debounce = false
end
end
end
if cmd == "GuardRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
DispatchRole = false
GuardRole = true
DriverRole = false
NoRole = false
if not debounce then
debounce = true
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
local clonedgui = GuardRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
local avantixclone = AvantixMobile:Clone()
avantixclone.Parent = player.Backpack
local whistleclone = Whistle:Clone()
whistleclone.Parent = player.Backpack
debounce = false
end
end
end
if cmd == "DriverRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
DriverRole = true
GuardRole = false
DispatchRole = false
NoRole = false
if not debounce then
debounce = true
for _,v in pairs(player.Backpack:GetChildren()) do
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
local clonedgui = DriverRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
debounce = false
end
end
end
if cmd == "DeleteRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
if not debounce then
debounce = true
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
debounce = false
DriverRole = false
GuardRole = false
DispatchRole = false
NoRole = true
end
end
end
end)
for _,v in pairs(player.Backpack:GetChildren()) do
if AvantixMobileFind then
v.Backpack.AvantixMobile:Destroy()
elseif DispatchBatonFind then
v.Backpack.DispatchBaton:Destroy()
elseif WhistleFind then
v.Backpack.Whistle:Destroy()
end
end
player.Backpack:GetChildren() will give you a table of all the children within the backpack. “v” will be given the value of one of the table values throughout the loop. This means the value of “v” is a tool. Your script does “v.Backpack” meaning it’s looking in the player’s backpack for a tool and then in that tool’s backpack for the tool. Your script already checks if the backpack has the tools, therefore you don’t need the loop
if AvantixMobileFind then
AvantixMobileFind:Destroy()
elseif DispatchBatonFind then
DispatchBatonFind:Destroy()
elseif WhistleFind then
WhistleFind:Destroy()
end
You could make it so that your script is easier to change later on by putting a new table with all the find values in at at the top of the script, then looping around the table everywhere else in the script. (ALTERNATIVE TO ABOVE)
local ToolsToRemove = {WhistleFind, AvantixMobileFind, DispatchBatonFind}
for _, v in pairs(ToolsToRemove) do
if v then v:Destroy() end
end
While this isn’t a fix to getting so many, assuming you did the second part of the code, you could edit it so that rather than just deleting the tools founds, it would delete all tools with the same name found in the backpack.
for _,v in pairs(ToolsToRemove) do
for i, tool in pairs(backpack:GetChildren()) do
if tool.Name = v.Name then tool:Destroy() end
end
end
local DispatchRole = false
local DriverRole = false
local GuardRole = false
local NoRole = true
local ServerStorage = game:GetService("ServerStorage")
local DispatchRoleGUI = ServerStorage.OverheadRolesGUI.DispatchRole
local GuardRoleGUI = ServerStorage.OverheadRolesGUI.GuardRole
local DriverRoleGUI = ServerStorage.OverheadRolesGUI.DriverRole
local Whistle = ServerStorage.Tools.Whistle
local AvantixMobile = ServerStorage.Tools.AvantixMobile
local DispatchBaton = ServerStorage.Tools.DispatchBaton
local debounce = false
game.Workspace.OverheadGUIs.RemoteEvent.OnServerEvent:connect(function(player, cmd)
local WhistleFind = player.Backpack:FindFirstChild("Whistle")
local AvantixMobileFind = player.Backpack:FindFirstChild("AvantixMobile")
local DispatchBatonFind = player.Backpack:FindFirstChild("DispatchBaton")
local backpack = player.Backpack
local ToolsToRemove = {WhistleFind, AvantixMobileFind, DispatchBatonFind}
if cmd == "DispatcherRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
for _, v in pairs(ToolsToRemove) do
if v then v:Destroy() end
end
DispatchRole = true
GuardRole = false
DriverRole = false
NoRole = false
if not debounce then
debounce = true
local clonedgui = DispatchRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
local clonedwhistle = Whistle:Clone()
clonedwhistle.Parent = player.Backpack
local clonedbaton = DispatchBaton:Clone()
clonedbaton.Parent = player.Backpack
wait(1)
debounce = false
end
end
end
if cmd == "GuardRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
DispatchRole = false
GuardRole = true
DriverRole = false
NoRole = false
if not debounce then
debounce = true
for _, v in pairs(ToolsToRemove) do
if v then v:Destroy() end
end
local clonedgui = GuardRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
local avantixclone = AvantixMobile:Clone()
avantixclone.Parent = player.Backpack
local whistleclone = Whistle:Clone()
whistleclone.Parent = player.Backpack
wait(1)
debounce = false
end
end
end
if cmd == "DriverRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
DriverRole = true
GuardRole = false
DispatchRole = false
NoRole = false
if not debounce then
debounce = true
for _, v in pairs(ToolsToRemove) do
if v then v:Destroy() end
end
local clonedgui = DriverRoleGUI:Clone()
clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
wait(1)
debounce = false
end
end
end
if cmd == "DeleteRole" then
for i,v in pairs (player.Character.Head:GetChildren()) do
if v:IsA("BillboardGui")then
v:Destroy()
end
if not debounce then
debounce = true
for _, v in pairs(ToolsToRemove) do
if v then v:Destroy() end
end
wait(1)
debounce = false
DriverRole = false
GuardRole = false
DispatchRole = false
NoRole = true
end
end
end
end)
Okay - for sake of ease (in finding parts of the code) which command did you use?
EDIT: Can I have a look at the script related to firing the remote event as well?
By command, I meant that when you run the server event function, there’s something called “cmd”, I’m asking for the value of “cmd” - it’s not necessary since all parts of the code are basically the same - though it’d help narrow searching through parts of the code in case there was a difference.
Though, the local script that you’re using to fire the server event is important to look at.