i have been creating an arcade game, and i created all the scripts except for two. The 2d camera, and 2d movement from Gnomecode’s youtube tutorial. i recently realized after i fixed the camera script that the control script was not performing well. i would like if you could review this code and tell me what makes it so laggy.
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local collection = game:GetService("CollectionService")
local jumping = false
local crouching = false
local already_destroyed = false
local leftValue, rightValue = 0, 0
local function onLeft(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
leftValue = 1
elseif inputState == Enum.UserInputState.End then
leftValue = 0
end
end
local function onRight(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
rightValue = 1
elseif inputState == Enum.UserInputState.End then
rightValue = 0
end
end
local function onJump(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
jumping = true
elseif inputState == Enum.UserInputState.End then
jumping = false
end
end
local function onCrouch(actionName,inputState)
if inputState == Enum.UserInputState.Begin then
crouching = true
already_destroyed = false
end
if inputState == Enum.UserInputState.End then
crouching = false
end
end
local function onUpdate()
if player.Character and player.Character:FindFirstChild("Humanoid") then
if jumping then
player.Character.Humanoid.Jump = true
crouching = false
end
if crouching then
local id = "rbxassetid://5578912143"
local anim = Instance.new("Animation")
anim.AnimationId = id
local loaded = player.Character.Humanoid:LoadAnimation(anim)
loaded:Play()
player.Character.Humanoid.WalkSpeed = 0
collection:AddTag(player.Character,"crouching")
RunService.Heartbeat:Connect(function()
if not crouching then
if player.Character.Humanoid.Health <1 then return end
collection:RemoveTag(player.Character,"crouching")
loaded:Stop()
player.Character.Humanoid.WalkSpeed = 16
end
end)
end
local moveDirection = rightValue - leftValue
player.Character.Humanoid:Move(Vector3.new(moveDirection,0,0), false)
end
end
RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)
ContextActionService:BindAction("Left", onLeft, true, "A", Enum.KeyCode.DPadLeft,Enum.KeyCode.A)
ContextActionService:BindAction("Right", onRight, true, "D", Enum.KeyCode.DPadRight,Enum.KeyCode.D)
ContextActionService:BindAction("Jump", onJump, true, "W", Enum.KeyCode.W, Enum.KeyCode.Up, Enum.KeyCode.DPadUp, Enum.KeyCode.ButtonA)
ContextActionService:BindAction("crouch",onCrouch,true,"S",Enum.KeyCode.S)
please do not link me to gnomecodes’ tutorial as it is what’s causing the issue.
thank you for your time.