How to detect multiple tools?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have a drive-by feature in the game I am currently making, but the drive by script works like this: You pull out a tool & it plays a animation for you to peak out the car.
The issue is that no matter what tool you pull out, it still plays the animation. I only want the script to play an animation if its a certain tool like a gun.

Here is the script if you want to see.



local seat = script.Parent
local childAddedConn, AncestryChangeConn
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if childAddedConn then
		childAddedConn:Disconnect()
	end
	if AncestryChangeConn then
		AncestryChangeConn:Disconnect()
	end
	childAddedConn, AncestryChangeConn = nil, nil
	if seat.Occupant ~= nil then
		local humanoid = seat.Occupant
		local char = humanoid.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		childAddedConn = char.ChildAdded:Connect(function(tool)
			if tool:IsA("Tool") then
				game.ReplicatedStorage.LeftPeak:FireClient(player)
				tool.AncestryChanged:Wait()
				AncestryChangeConn = tool.AncestryChanged:Connect(function()
					game.ReplicatedStorage.StopPeak:FireClient(player)
					AncestryChangeConn:Disconnect()
				end)
			end
		end)
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried putting this in the script

local NotGuns = {
    player.Backpack.Burger
    player.Backpack.Soda
}

for i, notgun in pairs(NotGuns) do
    notgun.Enabled = false
end

But it didn’t seem to work, and had no errors in the output.
It would be appreciated if anybody could help me with this.

Try this out:

local NotGuns = {
    "Burger",
    "Soda"
}

for i, notgun in pairs(NotGuns) do
    player.Backpack[notgun].Enabled = false
end

And you have to but a comma after each value you put in a table (i hope you understand what i mean)
If not here is an example:

Example:

Wrong:

local Table = {

 "1"
 "2"

}

Right:

local Table = {

 "1",
 "2"

}
1 Like
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function isAGun(tool: Tool): boolean
	return tool:GetAttribute("IsAGun")
end

local seat = script.Parent
local ChildAdded_Connection

local function disconnect_Connections()
	if ChildAdded_Connection then
		ChildAdded_Connection:Disconnect()
	end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(function()

	disconnect_Connections()

	if seat.Occupant then
		local humanoid: Humanoid = seat.Occupant
		local character: Model = humanoid.Parent
		local player: Player = game.Players:GetPlayerFromCharacter(character)

		ChildAdded_Connection = character.ChildAdded:Connect(function(addedChild: Instance): RBXScriptConnection
			if addedChild:IsA("Tool") then
				local tool: Tool = addedChild
				if isAGun(tool) then
					ReplicatedStorage.LeftPeak:FireClient(player)
					tool.Unequipped:Wait()
					ReplicatedStorage.StopPeak:FireClient(player)
				end
			end
		end)
	end
end)

You should be using Attributes to determine which Tool is a Gun

2 Likes

So I would have to add a boolean attribute to the actual tool like this?
image

Nevermind, it works now. Thank you!