Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I tell the function to only work if no tool is selected?

I have a script and I only want it to work if the player has no tools selected. How could I define no tools being selected? I tried the player's Backpack.Selected and Bin.Selected but neither of those worked. Basically what I'm asking is, how can I say in Lua "when the player has no tool selected"? Thanks for any help guys and let me know if I need to clarify anything!

1 answer

I don't know of any built in API to do such a thing, but it is quite easy to make a function that checks for this.

When a tool is equipped, it is transferred from the Player's backpack into the Player's Character. Knowing this, we can loop through the children of the character in order to determine if a tool is equipped:

function isToolEquipped(player)
	local character = player.Character
	if character then
		for _, child in next, character:GetChildren() do
			if child:IsA("Tool") then
				return true
			end
		end
	end
	return false -- If it hasn't returned yet, no tool is equipped
end

So in your case, you could use this in an if statement like:

if not isToolEquipped(player) then

Assuming 'player' is defined

###EDIT: Hopperbin Version

function isToolEquipped(player)
	local character = player.Character
	if character then
		for _, child in next, character:GetChildren() do
			if child:IsA("Tool") then
				return true
			end
		end
	end

	for _, child in next, player.Backpack:GetChildren() do
		if child:IsA("HopperBin") and child.Active then
			return true
		end
	end

	return false -- If it hasn't returned yet, no tool is equipped
end

4 comments

0

It worked for a gear sword I had but not for the script based tools I have. I think they're called HopperBin tools maybe? I tried changing the child:IsA to "HopperBin" But that didn't work either

0

Sorry, changing it to BackpackItem should fix it as both Tools and HopperBins inherit from the class BackpackItem.

0

So it turns out that HopperBins do not get parented to the character. They are deprecated, so a Tool with it's property RequiresHandle set to false should be used. However, I will modify it to give a version that'll work with hopperbins.

0

See the edit

This discussion is preserved as it was. New answers, comments, and votes are closed.