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

Deleting Any Tools From a Character?

Asked by 9 years ago

So I already know how to find a character, but I want to make a script that will delete any tool that character is holding and replace it with a new one. All I really need is the command that finds all tools in a model... I know it's simple, but I'm new. If you can help, thanks!

1 answer

Log in to vote
0
Answered by 9 years ago

You can just use the UnequipTools function in the character's Humanoid to put away any tools the player is holding.

If you actually need to forcefully destroy any tools in the character, you can use a for loop to loop through every item in the character and then use an if statement along with the IsA function to check if each object is a Tool, and then destroys it.

--I will be using a variable called char to reference the character to show you that the character is needed here. Replace char with the path to the character to make it work as intended.

local charChildren = char:GetChildren() --Gets the children of the character.

for n = 1, #charChildren do --Repeats the code wrapped in the for loop by the amount of children in the character.
    if charChildren[n]:IsA("Tool") then --Check if the nth child of the character is a tool.
        charChildren[n]:Destroy() --Destroys the nth child that is a tool.
    end
end

To shorten the code above, you can use something called a generic for loop to reference the child directly.

--I will be using a variable called char to reference the character to show you that the character is needed here. Replace char with the path to the character to make it work as intended.

for i,v in pairs(char:GetChildren()) do --i is the position of the value in the table, v is the value, or the child that we're currently looking at.
    if v:IsA("Tool") then --Checks if v is a tool.
        v:Destroy() --Destroys v, the tool.
    end
end --See how we write less compared to the last piece of code.

I hope this answered your question. If it did, be sure to accept my answer.

Happy scripting!

0
Not working... Does the fact that it's in a local script in a GUI matter? Busterkiki01 0 — 9y
0
Possibly. Did you change char to the path of the player's character? Spongocardo 1991 — 9y
0
I'm actually using that same name for the same location, so yeah... Busterkiki01 0 — 9y
Ad

Answer this question