A return type function that tells if a player haves a tool or not

I believe that we should add a returning Instance-type function in the Player instance that would allow you to find if a player has a tool or not. This function works for the client and server.

What I think it could look like

Player:FindTool(string toolname)

What it will do is check for a tool instance with the name you provide in the parents Backpack, Startergear, or the character itself(to see if it’s equipped). If the code does find the tool instance, it will return that tool; If it fails then it returns nil.
Ideally what the function code will look like

function self:FindTool(toolname)
	--Since the function is from the Player instance, the Self is equal to that player
	local result --This will be returned at the end of the function, if we fail to find the tool you type in, it will just stay nil. Else we'll set the result variable to the tool
	
	--Since the client doesn't have a StarterGear, we need to check if this is the client or server
	local StarterGear = self:FindFirstChild("StarterGear")
	
	local Backpack = self:WaitForChild("Backpack")
	local Character = self.Character or self.CharacterAdded:Wait()
	
	--This if-else will check each parent for a child that is named the tool and is a tool instance
	
	--Checking in order: Starterpack, Backpack, Character
	for i = 1, 3 do
		--Selecting the parent we plan to look through
		local parent
		if i == 1 then
			parent = StarterGear
		elseif i == 2 then
			parent = Backpack
		elseif i == 3 then
			parent = Character
		end
		--This is just incased this is the client since we don't set a value for the parent when it was going to set it for StarterGear
		if parent then
			for _,v in ipairs(parent:GetChildren()) do
				if v:IsA("Tool") and v.Name == toolname then
					--This is the tool that matches our requirements, set the result to this tool and break out of the forloop completeley
					result = v
					break
				end
			end
		end
	end
	
	--If we found the tool, it will return the tool. If we failed to find any tool then it will return nil
	return result
end

Why should this be added
This would make checking for if players have a tool or not easier and finding tools in players simple and more efficient than just doing something like

local tool = player.Backpack:FindFirstChild(toolname) or player.Character:FindFirstChild(toolname)

Example of function in use


	--[[
		Example: Say I want to give this player a tool named Moneybag from the serverstorage
		but also don't want to give him/her one if they already have it. This is where I can use
		the FindTool function and see if we can find an instance of this tool.
	--]]
	
	--Getting the moneybag tool
	local Moneybag = game:GetService("ServerStorage"):WaitForChild("Moneybag")
	--This variable is to see if I get the tool instance or a nil
	local alreadyHaveTool = Player:FindTool("Moneybag")
	--If alreadyHaveTool doesn't return nil then you already have the tool and I don't want to give you the same tool
	if not alreadyHaveTool then
		--Give the bag tool to the player's backpack
		Moneybag:Clone().Parent = plr.Backpack
	end

This topic was automatically closed after 1 minute. New replies are no longer allowed.