-- Compiled with roblox-ts v3.0.0 local TS = _G[script] local Players = TS.import(script, TS.getModule(script, "@rbxts", "services")).Players local FunctionTools = TS.import(script, script.Parent, "function_tools").FunctionTools local InstanceTools local InstanceTools = {} do local _container = InstanceTools local random = Random.new() --[[ * * creates instance * @param class_name * @param properties partial properties * @returns created instancce ]] local AsignProperties local function Create(class_name, properties, callback) --creates the instance local instance = Instance.new(class_name) --assigns the properties AsignProperties(instance, properties) local _result = callback if _result ~= nil then _result(instance) end return instance end _container.Create = Create --[[ * * destroys all children of instance * @param instance * @returns ]] local function ClearChildren(instance) for _, child in instance:GetChildren() do child:Destroy() end return instance end _container.ClearChildren = ClearChildren --[[ * * @param instance * @param properties partial properties * @returns ]] function AsignProperties(instance, properties) --needs the type cast to set the properties to the instance local writable_instance = instance local properties_map = properties for property_name, value in properties_map do writable_instance[property_name] = value end return instance end _container.AsignProperties = AsignProperties --[[ * * checks if the descendant of some of the instances * @param instance * @param possible_ancestor_list possible ancestors * @returns ]] local function IsDescendantOf(instance, possible_ancestor_list) for _, ancestor in possible_ancestor_list do if instance:IsDescendantOf(ancestor) then return true end end return false end _container.IsDescendantOf = IsDescendantOf --[[ * * loads a copy of settings from module script as an interface, and takes missing values from default settings * @param module_script module script where are the settings * @param default_settings default settings * @returns copy of settings loaded from module script ]] local function LoadSettingsFromModuleScript(module_script, default_settings) local settings = require(module_script) --creates a copy of data local settings_copy = {} --converts default settings to a map --itterates over it and looks for the same settings in module script for name, default_setting in default_settings do --sets the value of module script or of default settings local _condition = settings[name] if _condition == nil then _condition = default_setting end settings_copy[name] = _condition end return settings_copy end _container.LoadSettingsFromModuleScript = LoadSettingsFromModuleScript --*finds the ancestor that meets the criteria of the check function local function FindAncestor(instance, check_function) while instance.Parent ~= nil do --sets the parent of the instance instance = instance.Parent --checks the parent and returns if passed a check if check_function(instance) then return instance end end end _container.FindAncestor = FindAncestor --*looks for the child where check_function return true local function FindChild(instance, check_function, recursive) local children = if recursive then instance:GetDescendants() else instance:GetChildren() local _check_function = check_function -- ▼ ReadonlyArray.find ▼ local _result for _i, _v in children do if _check_function(_v, _i - 1, children) == true then _result = _v break end end -- ▲ ReadonlyArray.find ▲ return _result end _container.FindChild = FindChild --[[ *looks for the ancestor which is a character that belongs to a player * @param check_full_ancestry if the part is deep in the character e.g Head.OtherPart.ThisPart, can be used to detect the character ]] local function GetPlayerCharacterFromInstace(part, check_full_ancestry) if not check_full_ancestry then return if Players:GetPlayerFromCharacter(part.Parent) ~= nil then (part.Parent) else nil end local character = FindAncestor(part, function(instance) return instance:IsA("Model") and Players:GetPlayerFromCharacter(instance) ~= nil end) return character end _container.GetPlayerCharacterFromInstace = GetPlayerCharacterFromInstace --*looks for the ancestor which is a character and returns a player associated with it local function GetPlayerFromInstance(part, check_full_ancestry) return Players:GetPlayerFromCharacter((GetPlayerCharacterFromInstace(part, check_full_ancestry))) end _container.GetPlayerFromInstance = GetPlayerFromInstance --*checks the backpack and the character local function SearchToolInPlayer(player, tool_name) local search_function = function(instance) return instance:IsA("Tool") and instance.Name == tool_name end --first looks in the backpack for the tool named tool_name local backpack = player:WaitForChild("Backpack") local tool = FindChild(backpack, search_function) if tool ~= nil then return tool end --if didnt find the tool, looks in the player character if the character is not nil if player.Character == nil then return nil end return FindChild(player.Character, search_function) end _container.SearchToolInPlayer = SearchToolInPlayer --*clears all the tags from the instance local function RemoveTags(instance) local tags = instance:GetTags() for _, tag in tags do instance:RemoveTag(tag) end end _container.RemoveTags = RemoveTags --*cleans all the the tags from the instance and it's descendants local function DeepRemoveTags(instance) local descendants = instance:GetDescendants() for _k, _v in descendants do RemoveTags(_v, _k - 1, descendants) end RemoveTags(instance) end _container.DeepRemoveTags = DeepRemoveTags local function WaitForPath(instance, path) local child = instance for _, name in path do --used for error handling local previous = child --if the name is .. go to parent, otherwise find first child child = if name == ".." then child.Parent else child:WaitForChild(name) local _arg0 = child ~= nil local _arg1 = `{previous:GetFullName()} doesnt have parent` assert(_arg0, _arg1) end return child end _container.WaitForPath = WaitForPath local function FindFirstOnPath(instance, path) local child = instance for _, name in path do --if the name is .. go to parent, otherwise find first child child = if name == ".." then child.Parent else child:FindFirstChild(name) --returns if something on the path is nil if child == nil then return nil end end return child end _container.FindFirstOnPath = FindFirstOnPath --*returns random position in part local function GetRandomPositionInPart(part) local unit_vector = random:NextUnitVector() local half_size = part.Size * 0.5 local position = part.Position local _arg0 = half_size * unit_vector return position + _arg0 end _container.GetRandomPositionInPart = GetRandomPositionInPart --[[ * * creates the item for the server and client, has to be called from the both sides * @param name * @param parent * @param class_name * @param properties * @returns ]] local function DefineItem(name, parent, class_name, properties) local instance = FunctionTools.ExecuteOnServerAndClient(function() local _arg0 = parent:FindFirstChild(name) == nil local _arg1 = `Instance with name {name} is already in {parent:GetFullName()}` assert(_arg0, _arg1) local instance = Create(class_name, properties) instance.Name = name instance.Parent = parent return instance end, function() local instance = parent:WaitForChild(name) return instance end) return instance end _container.DefineItem = DefineItem --[[ * * gets or creates the instance of type * @param name * @param parent * @param class_name * @param properties * @returns ]] local function GetOrCreate(name, parent, class_name, properties) local instance = parent:FindFirstChild(name) if instance ~= nil then return instance end instance = InstanceTools.Create(class_name, properties) instance.Name = name instance.Parent = parent return instance end _container.GetOrCreate = GetOrCreate end return { InstanceTools = InstanceTools, }