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!
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
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