Connections inside a connection doesn't fire

I’m trying to make a tool module using ChildAdded, however the Tool’s connections doesn’t seem to fire whenever, but the Connection variable isn’t nil.

Character.ChildAdded:Connect(function(Tool)
	if Tool:IsA("Tool") and Tool:FindFirstChild("Stuff") then
		print(Tool.Animations:GetChildren())
		print(require(Tool.Stuff))
		Tool.Name = require(Tool.Stuff).Name
		Connection = Tool.Activated:Connect(function()
			print("Activated")
			print("Tool Damage", require(Tool.Stuff).Damages.Normal)
		end)
		Tool.Unequipped:Once(function()
			print("yeah")
		end)
	end
end)

I am activating the tool as evident from the InputObject output.
image

Hey there, @weakroblox35

I noticed you’re encountering some issues with the Character.ChildAdded event and tool connections in your script. I’ll do my best to assist you with this.

Let’s take a closer look at the code you provided:

Your Code:
Character.ChildAdded:Connect(function(Tool)
    if Tool:IsA("Tool") and Tool:FindFirstChild("Stuff") then
        print(Tool.Animations:GetChildren())
        print(require(Tool.Stuff))
        Tool.Name = require(Tool.Stuff).Name
        Connection = Tool.Activated:Connect(function()
            print("Activated")
            print("Tool Damage", require(Tool.Stuff).Damages.Normal)
        end)
        Tool.Unequipped:Once(function()
            print("yeah")
        end)
    end
end)

I’ve identified a couple of issues in your code:

  1. The Connection variable is being overwritten each time a tool is added, limiting you to only one active connection. Consider using a table to manage multiple connections if needed.
  2. The usage of Tool.Unequipped:Once(...) might not be correct. As far as I know, there’s no Once method for the Tool object. You should use the Connect method instead.
Revised Code:
Character.ChildAdded:Connect(function(Tool)
    if Tool:IsA("Tool") and Tool:FindFirstChild("Stuff") then
        print(Tool.Animations:GetChildren())
        print(require(Tool.Stuff))
        Tool.Name = require(Tool.Stuff).Name
        local Connections = {}

        Connections.Activated = Tool.Activated:Connect(function()
            print("Activated")
            print("Tool Damage", require(Tool.Stuff).Damages.Normal)
        })

        Connections.Unequipped = Tool.Unequipped:Connect(function()
            print("Tool Unequipped")
            Connections.Activated:Disconnect() -- Disconnect the Activated connection when the tool is unequipped
        end)
    end
end)

In this revised code:

  1. We’ve introduced a Connections table to manage the various connections, allowing you to handle multiple connections without overwriting them.
  2. We replaced Tool.Unequipped:Once(...) with Tool.Unequipped:Connect(...). We also added code to disconnect the Activated connection when the tool is unequipped.

I hope this helps you resolve the issues you’re facing. If you have any questions or run into any problems while testing this code, feel free to ask for further assistance. :stuck_out_tongue_winking_eye::+1:

Best regards,
[Zero]

Um? I’m using Once because it’ll automatically disconnect the connection since well, unequipped is only needed once in this case of scenario, but still, the connections still wouldn’t fire.

local ConnectionActivated
local character = game.Players.LocalPlayer.Character

character:WaitForChild("Humanoid").ChildAdded:Connect(function(Tool)
	if Tool:IsA("Tool") then
		-- Check if it's a tool.

		-- Access the Tool's Handle (Assuming it's named "Handle").
		local Handle = Tool:FindFirstChild("Handle")

		if Handle and Handle:FindFirstChild("Stuff") then
			-- Check if the Handle exists and if it has a child named "Stuff".

			-- Print the children of the Animations object within the Handle.
			local Animations = Handle:FindFirstChild("Animations")
			if Animations then
				print(Animations:GetChildren())
			end

			-- Print the required "Stuff" module (assuming it contains valid data).
			local Stuff = Handle:FindFirstChild("Stuff")
			if Stuff then
				print(require(Stuff))
			end

			-- Set the name of the tool to the name specified in the "Stuff" module.
			if Stuff and Stuff:FindFirstChild("Name") then
				Tool.Name = Stuff.Name.Value
			end

			-- Connect to the Activated event of the tool.
			ConnectionActivated = Tool.Activated:Connect(function()
				-- When the tool is activated...
				print("Activated")

				-- Print the tool damage from the "Stuff" module (if it exists).
				if Stuff and Stuff:FindFirstChild("Damages") then
					print("Tool Damage", Stuff.Damages.Normal.Value)
				end
			end)
		end

		-- Connect to the Unequipped event of the tool.
		Tool.Unequipped:Connect(function()
			-- When the tool is unequipped...
			print("yeah")

			-- Disconnect the Activated connection when the tool is unequipped.
			if ConnectionActivated then
				ConnectionActivated:Disconnect()
			end
		end)
	end
end)

This script listens for the addition of tools to the character. When a tool is added, it checks if it’s a tool and whether it contains a child named “Stuff.” If both conditions are met, it performs the following actions:

  1. It prints the children of the Animations object within the tool.
  2. It prints the information from the required “Stuff” module (assuming the module contains valid data).
  3. It sets the name of the tool to the name specified in the “Stuff” module.
  4. It establishes two variables, ConnectionActivated and ConnectionUnequipped, to store event connections.
  5. It connects to the Activated event of the tool, allowing it to print messages and data when the tool is activated.
  6. It connects to the Unequipped event of the tool. When the tool is unequipped, it prints a message and manually disconnects the Activated connection.

I am still not sure though where you got :Once() from as it does not exist in the ROBLOX API as far as my knowledge goes. Although I hope this helps you resolve the issues you’re facing. If you have any questions or run into any problems while testing this code, feel free to ask for further assistance. :stuck_out_tongue_winking_eye::+1:

Best regards,
[Zero]

Hate to say this, but, are you saying AI? Once actually exists in here.

Also why use value for table values?

What were you attempting to achieve, and where does the problem arise?

The script does not have any issues, so I assume that an external factor may be causing this. Make sure that the tool has a handle or is configured with the property “Requires Handle off.” The connection will not trigger unless this condition is met.

Oh right, just forgot about this, my tool initially had no handle because I was just testing internally.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.