How to get a players tool

Ok so the title doesn’t really describe it all but what I want my tool to do is to be able to detect all the tools a player has (done) and then pick a random tool

local Tool = script.Parent
Tool.Activated:Connect(function()
	local Char = script.Parent.Parent
	local Player = game.Players:FindFirstChild(Char.Name)
	
	local tools = Player.Backpack:GetChildren()
	local equipped = Player.Character:FindFirstChildOfClass("Tool")

	if equipped then
		table.insert(tools, equipped)
	end

	print(tools) 
	
	
end)

I don’t know how to pick a random tool from the table though

2 Likes

You can do it by indexing the “tools” table with Random.new():NextInteger().

local Tool = script.Parent

Tool.Activated:Connect(function()
	local Char = script.Parent.Parent
	local Player = game.Players:FindFirstChild(Char.Name)

	local tools = Player.Backpack:GetChildren()
	local equipped = Player.Character:FindFirstChildOfClass("Tool")

	if equipped then
		table.insert(tools, equipped)
	end

	local rand = tools[Random.new():NextInteger(1, #tools)]
	warn(rand)
end)
2 Likes