I cant figure out why this script makes my game so laggy, any tips?
local humanoid = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("Humanoid")
local hrp = game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("HumanoidRootPart")
_G.pushCooldown = false
_G.popcooldown = false
_G.Rotation = Instance.new("BodyGyro", hrp)
_G.Rotation.D = 250
_G.Rotation.MaxTorque = Vector3.new(0, 10000000, 0)
_G.Rotation.P = 10000
_G.Left = false
_G.Right = false
_G.Bent = false
_G.RstickDWN = false
_G.BendCD = false
function ChangeAnimation(anim)
humanoid:LoadAnimation(game.ReplicatedStorage.Animations[anim]):Play()
for _,v in pairs(humanoid:GetPlayingAnimationTracks()) do
if v.Name ~= "SkatePush" and v.Name ~= anim then
v:Stop()
end
end
end
--Turn
function UpdateAnimation()
if _G.Left == true and _G.Right == false then
ChangeAnimation("SkateLeanLeft")
elseif _G.Left == false and _G.Right == true then
ChangeAnimation("SkateLeanRight")
else
if game.Players.LocalPlayer:WaitForChild("Velocity").Value <= 0 then
ChangeAnimation("SkateIdle")
else
ChangeAnimation("SkateRide")
end
end
end
--Push
game:GetService("UserInputService").InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.Gamepad1 then
if key.KeyCode == Enum.KeyCode.ButtonA and _G.pushCooldown == false and _G.popcooldown == false then
_G.pushCooldown = true
_G.popcooldown = true
humanoid:LoadAnimation(game.ReplicatedStorage.Animations.SkatePush):Play()
game.Workspace.Events.Push:FireServer()
game.Workspace.Events.VibrateController:FireServer()
print("Fired Push Event")
wait(1)
_G.pushCooldown = false
_G.popcooldown = false
--More Turning
elseif key.KeyCode == Enum.KeyCode.ButtonL2 and humanoid.FloorMaterial ~= Enum.Material.Air then
_G.Left = true
UpdateAnimation()
elseif key.KeyCode == Enum.KeyCode.ButtonR2 and humanoid.FloorMaterial ~= Enum.Material.Air then
_G.Right = true
UpdateAnimation()
end
end
game:GetService("UserInputService").InputEnded:Connect(function(key)
if key.KeyCode == Enum.KeyCode.ButtonL2 and humanoid.FloorMaterial ~= Enum.Material.Air then
_G.Left = false
UpdateAnimation()
elseif key.KeyCode == Enum.KeyCode.ButtonR2 and humanoid.FloorMaterial ~= Enum.Material.Air then
_G.Right = false
UpdateAnimation()
end
end)
end)
--Bend Knees
game:GetService("UserInputService").InputChanged:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Thumbstick2 and _G.BendCD == false and _G.pushCooldown == false and _G.popcooldown == false and _G.Bent == false and humanoid.FloorMaterial ~= Enum.Material.Air then
if input.Position.Y == -1 then
humanoid:LoadAnimation(game.ReplicatedStorage.Animations.BendKnees):Play()
_G.pushCooldown = true
_G.popcooldown = true
_G.Bent = true
_G.BendCD = true
game.Workspace.Events.BendKnees:FireServer()
print("Fired BendKnees Event")
end
end
--Unbend Knees
game:GetService("UserInputService").InputChanged:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Thumbstick2 and _G.BendCD == true and _G.pushCooldown == true and _G.popcooldown == true and _G.Bent == true and humanoid.FloorMaterial ~= Enum.Material.Air then
if input.Position.Y ~= -1 then
humanoid:LoadAnimation(game.ReplicatedStorage.Animations.SkateRide):Play()
UpdateAnimation()
_G.pushCooldown = false
_G.popcooldown = false
_G.Bent = false
game.Workspace.Events.UnBendKnees:FireServer()
print("Fired UnbendKnees Event")
wait(.5)
_G.BendCD = false
end
end
end)
--Ollie
game:GetService("UserInputService").InputChanged:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Thumbstick1 and _G.pushCooldown == true and _G.popcooldown == true and _G.Bent == true and humanoid.FloorMaterial ~= Enum.Material.Air then
if key.Position.Y == 1 and input.Position.Y == -1 then
_G.pushCooldown = false
_G.Bent = false
_G.popcooldown = false
game.Workspace.Events.Ollie:FireServer()
game.Workspace.Events.VibrateController:FireServer()
print("Fired Ollie")
humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Tricks.TrickPlaceHolder):Play()
end
end
--HeelFlip
game:GetService("UserInputService").InputChanged:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Thumbstick1 and _G.pushCooldown == true and _G.popcooldown == true and _G.Bent == true and humanoid.FloorMaterial ~= Enum.Material.Air then
if key.Position.X == 1 and input.Position.Y == -1 then
_G.pushCooldown = false
_G.Bent = false
_G.popcooldown = false
game.Workspace.Events.HeelFlip:FireServer()
game.Workspace.Events.VibrateController:FireServer()
print("Fired HeelFlip")
humanoid:LoadAnimation(game.ReplicatedStorage.Animations.Tricks.TrickPlaceHolder):Play()
end
end
end)
end)
end)