Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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")

5 comments

0

don't understand your question. Do you mean different variables for each tool? Like local Pistol = player.Backpack:FindFirstChild("Pistol")

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

0

Instead of local pistol = game.Workspace[player.Name].Pistol maybe local tool = player.Backpack:FindFirstChildWhichIsA("Tool")?

0

ohh so one variable for both of the tools?

0

YES!!!!

1 answer

✓ Accepted answer

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

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.