I have this tool that is tagged with “Pickaxe” tag and i’m trying to get the .activated event from a for loop, but for some reason it just never prints out the stuff inside the activate event. Everything before that prints fine.
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickaxeService = require(ReplicatedStorage.Shared.Services.Pickaxes)
local player = game.Players.LocalPlayer
local pickaxe_tagged = CollectionService:GetTagged("Pickaxe")
for _, pickaxe in ipairs(pickaxe_tagged) do
if not pickaxe then
warn("Pickaxe not found")
end
pickaxe.Activated:Connect(function()
PickaxeService:OnActivated(pickaxe)
end)
end
Module if needed :
local Pickaxes = {}
function Pickaxes:OnActivated(pickaxe)
print("test")
end
return Pickaxes
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickaxeService = require(ReplicatedStorage.Shared.Services.Pickaxes)
local player = game.Players.LocalPlayer
local pickaxe_tagged = CollectionService:GetTagged("Pickaxe")
local function add(pickaxe)
if not pickaxe then
warn("Pickaxe not found")
end
pickaxe.Activated:Connect(function()
PickaxeService:OnActivated(pickaxe)
end)
end
CollectionService:GetInstanceAddedSignal("Pickaxe"):Connect(add)
for _, pickaxe in ipairs(pickaxe_tagged) do add(pickaxe) end
Also I don’t think you need to check if not pickaxe because the instance will never be nil
The issue was that your script was connecting the .Activated event on every tagged pickaxe in the game, even the ones sitting in ReplicatedStorage/StarterPack, those cannot be equiped, so the event never fires.
Here’s a fix:
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PickaxeService = require(ReplicatedStorage.Pickaxes) -- I changed the name for testing lol
local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
for _, pickaxe in ipairs(CollectionService:GetTagged("Pickaxe")) do
if pickaxe:IsDescendantOf(backpack) or pickaxe:IsDescendantOf(player.Character or player.CharacterAdded:Wait()) then
pickaxe.Activated:Connect(function()
PickaxeService:OnActivated(pickaxe.Name) --temp changed to pickaxe.Name so you see in case u have more that one tool which tool based on it's name is firing the event
end)
end
end
Make sure you run this from a LocalScript with a part named Handle, or if no parts are in it directly: take a look at the tool properties and make sure you have the Tags: “Pickaxe” created. Also if you have no part named handle in it, make sure you turn of the property Pickaxe->Behavior.RequiresHandle. It’s automatically set to true to require a handle part. Let me know if this fixed your issue.