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

How to assign a tool as a variable regardless of which tool it is?

Asked by 6 years ago

I have a Pistol tool and a SMG tool and I trying to write code for both. I can't get anything to work

local player = game.Workspace.LocalPlayer
local tool = player:FindFirstChildOfClass("Tool")

0
don't understand your question. Do you mean different variables for each tool? Like local Pistol = player.Backpack:FindFirstChild("Pistol") hellmatic 1523 — 6y
0
I have a gui module that changes values of a weapon tool. I have it working but now I want to add more weapons with different names, without having to rewrite the code. local pistol = game.Workspace[player.Name].Pistol --this is how it is now 0oEagleo0 -3 — 6y
0
Instead of local pistol = game.Workspace[player.Name].Pistol maybe local tool = player.Backpack:FindFirstChildWhichIsA("Tool")? 0oEagleo0 -3 — 6y
0
ohh so one variable for both of the tools? hellmatic 1523 — 6y
0
YES!!!! 0oEagleo0 -3 — 6y

1 answer

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago

There's multiple ways you can do this.

Table:

local tools = {
    ["Pistol"] = player.Backpack:FindFirstChild("Pistol"),
    ["SMG"] = player.Backpack:FindFirstChild("SMG"),
}

print(tools.Pistol, tools.SMG)

Looping:

for i, v in pairs(player.Backpack:GetChildren()) do -- gets the backpack's children and checks if the child (v) is a tool
    if v:IsA("Tool") then 
        local tool = v
        -- code
    end
end
0
Wow, ok I thought tool was like a class name or something and it could be found with one line. No wonder... I would have never found this +1 to you bro/sis 0oEagleo0 -3 — 6y
Ad

Answer this question