I’m currently on making my game more optimised and cutting down on memory usage.
This is the most major optimisation of code I’ve made so far and I’d just like to know if I’m going in the right direction when it comes to optimisation.
local BP = script.Parent.Parent
local MM = BP:WaitForChild("Module_Management")
local Animation = MM:WaitForChild("Animations")
local SAnim = Animation:WaitForChild("Shoot")
local TM = require(BP:WaitForChild("ToolManagment"))
local Tween = require(MM:WaitForChild("Tweening"))
local animm = require(Animation)
local anim = require(SAnim).Q_Down
local anim2 = require(SAnim).Q_Up
local Kick = false
local Equipped = false
local Tool = script.Parent
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local power = 0
local Angle = 0
local percent = 0
local enabled = false
local Direction = Enum.EasingDirection.Out
local Style = Enum.EasingStyle.Back
local Direction2 = Enum.EasingDirection.InOut
local Style2 = Enum.EasingStyle.Sine
local Duration1 = 0.15
local Duration2 = 0.2
local Duration3 = 0.3
Tool.Equipped:Connect(function()
Equipped = true
end)
Tool.Unequipped:Connect(function()
Equipped = false
end)
local Character = Player.Character
if not Character or Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local HRP = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
local CC = workspace.CurrentCamera
local RA = "Right Arm"
local LA = "Left Arm"
local Key = "q"
local function Force(Ball)
Kick = false
local Angle = Vector3.new(math.huge, math.huge, math.huge)
local AngleBar = HRP.CFrame.LookVector * power * 1.15
local force
if Angle >= 2 then
force = AngleBar + Vector3.new(0, Angle + 10, 0)
else
force = AngleBar + Vector3.new(0, Angle, 0)
end
TM.ApplyForce(Ball, Angle, force, "HRP")
end
local function alreadytouched(Limb)
for Table, Ball in pairs(Limb:GetTouchingParts()) do
if Ball ~= nil and Ball.Name == "TPS" and Kick then
Force(Ball)
end
end
Limb.Touched:Connect(function(Hit)
if Hit ~= nil and Hit.Name == "TPS" and Kick then
Force(Hit)
end
end)
Limb.TouchEnded:Connect(function(Hit)
if Hit ~= nil and Hit.Name == "TPS" and Kick then
Force(Hit)
end
end)
end
local function Calculate()
local Value
local Calc = math.asin((Head.Position.Y - CC.CFrame.Position.Y) / (Head.Position - CC.CFrame.Position).Magnitude)
if Calc < math.rad(0) then
return 0
end
if math.rad(0) < Calc then
if not (math.rad(25) < Calc) then
if Calc < math.rad(-55) then
Calc = math.rad(-22)
end
Value = Calc
return Value
end
else
Value = Calc
return Value
end
return math.rad(25)
end
Mouse.KeyDown:Connect(function(key)
if Equipped == false then return end
if TM.GetUsing() then return end
if key ~= Key then return end
TM.SetUsing(true)
TM.showPowerBar()
TM.setWindUp(true)
Tween.TweenWeld(RA, CFrame.new(1.5, 0, 0), CFrame.new(1.2, 0.5, -0.5) * CFrame.fromEulerAnglesXYZ(-math.pi / 1, -5, 5), Direction2, Style2, Duration2)
Tween.TweenWeld(LA, CFrame.new(-1.5, 0, 0), CFrame.new(-1.2, 0.5, -0.5) * CFrame.fromEulerAnglesXYZ(-math.pi / 1, 5, -5), Direction2, Style2, Duration2)
for i, v in pairs(anim) do
Tween.TweenWeld(i, animm[i], CFrame.new(0, 1.5, 0.2) * CFrame.fromEulerAnglesXYZ(Calculate(), 0, 0), Direction, Style, Duration1)
end
power = 35
Angle = 0
percent = 0
enabled = true
for i = 1, 20 do
if TM.getWindUp() then
power = power + 3.2
Angle = Angle + 0.05
TM.powerUp(i / 20)
TM.AdjustPowerBarColour(255/20)
local calculation = 100 / 20
percent = percent + calculation
Player.PlayerGui.Start.Bar.PowerBar.Percent.Text = "<i>"..percent.."%</i>"
task.wait(0.03)
end
end
end)
Mouse.KeyUp:Connect(function(key)
if TM.getWindUp() and TM.GetUsing() and Equipped == true and key == Key and enabled then
enabled = false
TM.setWindUp(false)
Kick = true
for i, v in pairs(anim) do
for i2, v2 in pairs(anim2) do
if i == i2 then
Tween.TweenWeld(i, v, CFrame.new(0, 1.5, -0.4) * CFrame.fromEulerAnglesXYZ(Calculate(), 0, 0), Direction, Style, Duration1)
end
end
end
alreadytouched(Head)
task.wait(0.5)
TM.resetPowerBar()
for i2, v2 in pairs(anim2) do
Tween.TweenWeld(i2, CFrame.new(0, 1.5, -0.4) * CFrame.fromEulerAnglesXYZ(Calculate(), 0, 0), animm[i2], Direction, Duration3)
end
Tween.TweenWeld(RA, CFrame.new(1.2, 0.5, -0.5) * CFrame.fromEulerAnglesXYZ(-math.pi / 1, -5, 5), CFrame.new(1.5, 0, 0), Direction, Style2, Duration3)
Tween.TweenWeld(LA, CFrame.new(-1.2, 0.5, -0.5) * CFrame.fromEulerAnglesXYZ(-math.pi / 1, 5, -5), CFrame.new(-1.5, 0, 0), Direction, Style2, Duration3)
task.wait(0.25)
Kick = false
TM.ResetWelds()
TM.SetUsing(false)
end
end)