What I dislike more than anything in programming is having to copy & paste similar pieces of code across multiple places. For example, I have dozens of tools that need to access a humanoid from the client. Copy-pasting them all would be terrible. Having to find a module script would be less terrible but still. Rather, I created a script that runs immediately that populates the shared
table with some utility items, to avoid boilerplate in my tools.
What do you guys think?
assert(script.Parent.ClassName == "ReplicatedFirst")
shared[-3] = Vector3.new(nil, -3)
do
local Slash = Instance.new("StringValue")
Slash.Name = "toolanim"
Slash.Value = "Slash"
shared.Slash = Slash
end
local Player = game:GetService("Players").LocalPlayer
local Anim = Instance.new("Animation")
local Max = Vector3.new(6e4, 6e4, 6e4)
local Humanoid
local Root
local function Added(Char:Model)
local Humanoid = Char:FindFirstChildOfClass("Humanoid")
if not Humanoid then
repeat
Humanoid = Char.ChildAdded:Wait()
until Humanoid.ClassName == "Humanoid"
end
return Humanoid
end
local function RootAdded(Humanoid:Humanoid)
local Root = Humanoid.RootPart
if not Root then
Humanoid:GetPropertyChangedSignal("RootPart"):Wait()
return Humanoid.RootPart
end
return Root
end
shared.HumanoidAdded = Added
shared.RootAdded = RootAdded
shared.Mouse = Player:GetMouse()
function shared.CharacterAdded()
return Player.Character or Player.CharacterAdded:Wait()
end
function shared.BodyPosition(Time, Position)
local Pos = Instance.new("BodyPosition")
Pos.Position = Position or Root.Position
Pos.MaxForce = Max
Pos.Parent = Root
task.wait(Time)
Pos:Destroy()
end
function shared.Animation(AnimId)
Anim.AnimationId = "rbxassetid://"..AnimId
local Track = Humanoid:LoadAnimation(Anim)
Anim.AnimationId = string.char()
Track:Play()
return Track
end
Player.CharacterAdded:Connect(function(Char)
shared.Character = Char
Humanoid = Added(Char)
shared.Humanoid = Humanoid
Root = RootAdded(Humanoid)
shared.RootPart = Root
end)