--The Name of this ModuleScript is "UtilityModule"
--This ModuleScript is found inside ServerScriptService
local UtilityModule = {}
function UtilityModule.WaitForChildWhichIsA(parent, className)
assert(typeof(parent) == "Instance", "Parent must be an Instance")
assert(type(className) == "string", "Class name must be a string")
local child = parent:FindFirstChildWhichIsA(className)
while not child or not child:IsA(className) do
child = parent.ChildAdded:Wait()
end
return child
end
return UtilityModule
Script that uses the function:
local UtilityModule = require(game.ServerScriptService.UtilityModule)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local Humanoid = UtilityModule.WaitForChildWhichIsA(character, "Humanoid")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
print(Humanoid)
print(HumanoidRootPart)
local equippedTool = nil
while true do
local newEquippedTool = UtilityModule.WaitForChildWhichIsA(character, "Tool")
if equippedTool ~= newEquippedTool then
equippedTool = newEquippedTool
print("Equipped Tool:", equippedTool)
end
wait()
end
end)
end)
In this example, we are using the function twice to show that it can be used more than once in the same function. I’m not sure if roblox has yet implemented a WaitForChildWhichIsA quick function but I hope this helps someone