I am working on a gun system for my upcoming game and I need to use the AncestoryChanged event on the tool. Still, for some reason, it is not being called when I listen for it in my main gun system server script located in ServerScriptService. I have tried placing a server script directly parented underneath the tool and then listening there and it works. I believe this happens because the tool gets cloned automatically when in StarterPack, but I thought events like AncestoryChanged were cloned with the instance.
If you have any advice on how to modify this to work, that would be awesome. I want to keep the server side underneath one script but if that is not possible, what should I do? I could parent another server script to the gun, but still, I would like to keep it underneath one server script as I have right now.
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- Modules
local GunController = require(script.GunController)
-- Classes
local GlobalSettings = require(script.GlobalSettings)
for _, folder: Folder in pairs(GlobalSettings.guns) do
for _, gun in pairs(folder:GetChildren()) do
if not gun:IsA("Tool") then continue end
local gunConfig = gun:FindFirstChildOfClass("ModuleScript")
if gunConfig and gunConfig.Name == "GunSettings" then
gun.AncestryChanged:Connect(function(_, parent)
print(parent)
end)
local setupGun = GunController.new(gun)
else
warn(`{gun.Name} is not a valid gun.`)
end
end
end