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")
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")
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
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
This discussion is preserved as it was. New answers, comments, and votes are closed.
5 comments
don't understand your question. Do you mean different variables for each tool? Like local Pistol = player.Backpack:FindFirstChild("Pistol")
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
Instead of local pistol = game.Workspace[player.Name].Pistol maybe local tool = player.Backpack:FindFirstChildWhichIsA("Tool")?
ohh so one variable for both of the tools?
YES!!!!