How to remove tools that may or may not be in a Player's backpack?

image

There are 2 other buttons for the other roles. Script are basically the same

As for what I said before, try put

 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

in the position of where the loop is:

This attempts to remove every tool with that name in the player’s backpack.

Im confused by my own code, would you be able to neaten it up a little bit for me?

Mk. I’ll try to and I’ll put the edit here, along with putting in that loop edit.
Hold up, your thing has ifs where elseifs would be usable - let me add.
Added - going to put in a new post.

1 Like

Ah, while I was editing it, I think I found out whats wrong - your code gives a tool for every instance within the player’s head as you forgot to end your loop. I’ll provide the code here after I finish.

1 Like

Here’s final version of the code. Hope this works and helps.

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 DispatchRole = false
local DriverRole = false
local GuardRole = false
local NoRole = true

local debounce = false

game.Workspace.OverheadGUIs.RemoteEvent.OnServerEvent:connect(function(player, cmd)

	local backpack = player.Backpack
	local WhistleFind = backpack:FindFirstChild("Whistle")
	local AvantixMobileFind = backpack:FindFirstChild("AvantixMobile")
	local DispatchBatonFind = backpack:FindFirstChild("DispatchBaton")
	
	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
		end
		
		DispatchRole = true
		GuardRole = false
		DriverRole = false
		NoRole = false
		
		if not debounce then
		debounce = true
		
		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 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
	elseif cmd == "GuardRole" then
		for i,v in pairs (player.Character.Head:GetChildren()) do
			if v:IsA("BillboardGui")then
				v:Destroy()
			end
		end
		
		DispatchRole = false
		GuardRole = true
		DriverRole = false
		NoRole = false
		
		if not debounce then
		debounce = true
		
		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 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
	elseif cmd == "DriverRole" then
		for i,v in pairs (player.Character.Head:GetChildren()) do
			if v:IsA("BillboardGui")then
				v:Destroy()
			end
		end
		
		DriverRole = true
		GuardRole = false
		DispatchRole = false
		NoRole = false
		if not debounce then
		debounce = true
		
		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 clonedgui = DriverRoleGUI:Clone()
		clonedgui.Parent = game.Workspace:WaitForChild(player.Name).Head
		wait(1)
		debounce = false
		end
	elseif cmd == "DeleteRole" then
		for i,v in pairs (player.Character.Head:GetChildren()) do
			if v:IsA("BillboardGui")then
				v:Destroy()
			end
		end
		
		if not debounce then
		debounce = true
		
		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
			
		wait(1)
		debounce = false
		DriverRole = false
		GuardRole = false
		DispatchRole = false
		NoRole = true
		end
	end
end)
4 Likes

Thank you, sooo much! It works perfectly!!!

1 Like

Your welcome. We all get moments when we mess up with the positioning of code. For me, I spent two days realising that I tried to use an object for FindFirstChild rather than a name :wink:

This also stresses the uses of indentation.

2 Likes