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

Check for tools with :GetChildren and for do not working?

Asked by
Aimarekin 345 Moderation Voter
6 years ago

Hi, I'm making a script that detects if a character is using a tool, and if it does, save the tool at a Value. I made this, but It seems like it doesn't work....

local children = plr.Character:GetChildren()
for i = 1, #children do
        if children[i].ClassName == "Tool" then
        local tool = children[i]
    end
end

It's inside a script and plr is a Player inside Players.

Could someone explain what am I doing wrong? Or if there is another more efficient way to get the tool a character is using, please tell me!

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago

You're using a local variable for tool, so it can only be used inside the if statement.

Read this wiki article about local variables

local tool

for i,v in pairs(plr.Character:GetChildren()) do
    if v:IsA("Tool") then
        tool = v
    end
end

print(tool)
0
Oh. IM DUMB. Aimarekin 345 — 6y
Ad

Answer this question