-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.luau
More file actions
250 lines (209 loc) · 4.95 KB
/
Copy pathinit.luau
File metadata and controls
250 lines (209 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
--!strict
local Inst = {}
local function safeAssign(instance: Instance & any, key: string, value: any) -- horrible hack to avoid errors
-- Prefer attributes for custom data
if instance:GetAttribute(key) ~= nil or key:match("^%u") == nil then
instance:SetAttribute(key, value)
return
end
-- Avoid errors when assigning properties
pcall(function()
(instance :: any)[key] = value
end)
end
--[=[
@class Inst
A lightweight utility for creating, cloning, and organizing Roblox Instances safely.
]=]
--[=[
Creates a new instance quickly with a name and optional parent.
```lua
local part = Inst.simple("Part", "MyPart", workspace)
```
]=]
function Inst.simple<T>(classname: string, name: string, parent: Instance?): T
local obj = Instance.new(classname) :: T & Instance
obj.Name = name
obj.Parent = parent
return obj
end
--[=[
Creates an instance with a table of properties applied safely.
```lua
local part = Inst.create("Part", {
Size = Vector3.new(4,4,4),
Anchored = true,
}, workspace)
```
]=]
function Inst.create<T>(
classname: string,
props: {[string]: any}?,
parent: Instance?
): T
local obj = Instance.new(classname) :: T & Instance
if props then
for k, v in props do
safeAssign(obj, k, v)
end
end
obj.Parent = parent
return obj
end
--[=[
Clones a template instance and optionally applies new properties.
```lua
local copy = Inst.cloneWith(template, { Color = Color3.new(1,0,0) }, workspace)
```
]=]
function Inst.cloneWith<T>(
template: T & Instance,
props: {[string]: any}?,
parent: Instance?
): T
local obj = template:Clone()
if props then
for k, v in props do
safeAssign(obj, k, v)
end
end
obj.Parent = parent
return obj
end
--[=[
Parents multiple children to the same instance.
```lua
Inst.children(model, { part1, part2, part3 })
```
]=]
function Inst.children(parent: Instance, children: {Instance})
for _, child in children do
child.Parent = parent
end
return parent
end
--[=[
Creates multiple instances at once.
```lua
local parts = Inst.batch("Part", 10, { Anchored = true }, workspace)
```
]=]
function Inst.batch(
classname: string,
amount: number,
props: {[string]: any}?,
parent: Instance?
): {Instance}
local list = {}
for i = 1, amount do
list[i] = Inst.create(classname, props, parent)
end
return list
end
--[=[
Finds the first descendant with the given name.
```lua
local button = Inst.firstDescendant(gui, "SubmitButton")
```
]=]
function Inst.firstDescendant(parent: Instance, name: string): Instance?
for _, desc in parent:GetDescendants() do
if desc.Name == name then
return desc
end
end
return nil
end
--[=[
Welds two parts together using a WeldConstraint.
```lua
Inst.weldParts(partA, partB)
```
]=]
function Inst.weldParts(part0: BasePart, part1: BasePart)
local weld = Instance.new("WeldConstraint")
weld.Part0 = part0
weld.Part1 = part1
weld.Parent = part0
return weld
end
--[=[
Welds multiple parts together to the first part in the list.
```lua
Inst.weldAll({ partA, partB, partC })
```
]=]
function Inst.weldAll(parts: {BasePart})
for i = 2, #parts do
Inst.weldParts(parts[1], parts[i])
end
end
--[=[
Assigns a property to an instance safely.
```lua
Inst.assignSafe(part, "Color", Color3.new(0,1,0))
```
]=]
function Inst.assignSafe(instance: Instance, key: string, value: any)
safeAssign(instance, key, value)
end
--[=[
Welds two parts together and aligns their CFrames.
```lua
Inst.weld(partA, partB)
```
]=]
function Inst.weld(part0: BasePart, part1: BasePart)
part1.CFrame = part0.CFrame
return Inst.weldParts(part0, part1)
end
--[=[
Clones a part, aligns it to another part, and welds them together.
```lua
local newPart = Inst.cloneAndWeld(partA, partB)
```
]=]
function Inst.cloneAndWeld(origin: BasePart, clone: BasePart)
local clonedPart = clone:Clone() :: BasePart
clonedPart.CFrame = origin.CFrame
clonedPart.Parent = origin.Parent
Inst.weldParts(origin, clonedPart)
return clonedPart
end
--[=[
Assigns multiple attributes to an instance from a table.
```lua
Inst.assignAttributesFromTable(part, { Health = 100, IsActive = true })
```
]=]
function Inst.assignAttributesFromTable(instance: Instance, attrs: {[string]: any})
for key, value in attrs do
instance:SetAttribute(key, value)
end
end
--[=[
Assigns multiple properties to an instance from a table.
```lua
Inst.assignPropertiesFromTable(part, { Size = Vector3.new(2,2,2), Anchored = false })
```
]=]
function Inst.assignPropertiesFromTable(instance: Instance, props: {[string]: any})
for key, value in props do
(instance :: any)[key] = value
end
end
function Inst.instanceOf(instance: Instance, className: string): boolean
return instance:IsA(className)
end
local destroyFunc = game.Destroy
--[=[
Adds an instance to Debris with a specified delay.
```lua
Inst.debrisAdd(part, 5) -- Destroys part after 5 seconds
```
]=]
function Inst.debrisAdd(instance: Instance, delay: number): Instance
task.delay(delay, destroyFunc, instance)
return instance
end
return Inst