Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

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
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 Gwolflover 80 — 8y
0
Sorry, changing it to BackpackItem should fix it as both Tools and HopperBins inherit from the class BackpackItem. BlackJPI 2658 — 8y
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. BlackJPI 2658 — 8y
0
See the edit BlackJPI 2658 — 8y
Ad

Answer this question