#How can I simplify this script while ensuring the snapping still works?

1 messages · Page 1 of 1 (latest)

obsidian quiver
#

How can I simplify this script while ensuring the snapping still works?

wanton charm
#

This has 18 less lines I don't know any context otherwise Id simplify it more

obsidian quiver
#

You got an extra end at the bottom of the bindAll function that's breaking the script, plus a hidden invisible character (a non-breaking space) right after Enum.ContextActionPriority.High.Value, that’ll make Luau freak out.

wanton charm
#

Again I don't know any context to what this is used for, Besides those two small things which were just typos the logic is correct

#
local CAS = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character, humanoid, rootPart

local MOVE_ACTION = "Move"
local JUMP_ACTION = "Jump"

-- Disable default controls
local PlayerModule = require(player.PlayerScripts:WaitForChild("PlayerModule"))
PlayerModule:GetControls():Disable()

-- Update character references
local function onCharacterAdded(char)
    character = char
    humanoid = char:WaitForChild("Humanoid")
    rootPart = char:WaitForChild("HumanoidRootPart")
    humanoid.AutoRotate = false
end

player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
    onCharacterAdded(player.Character)
end

-- Jump
local function onJump(_, state)
    if humanoid and state == Enum.UserInputState.Begin then
        humanoid.Jump = true
    end
    return Enum.ContextActionResult.Sink
end

-- Move
local function onMove(_, state, input)
    if not humanoid or not rootPart then return Enum.ContextActionResult.Pass end

    if state == Enum.UserInputState.End then
        humanoid:Move(Vector3.zero, false)
        return Enum.ContextActionResult.Sink
    end

    local dir = 0

    if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
        dir = -1
    elseif input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
        dir = 1
    end

    if dir ~= 0 then
        local moveVector = Vector3.new(dir, 0, 0)
        humanoid:Move(moveVector, false)

        local pos = rootPart.Position
        rootPart.CFrame = CFrame.lookAt(pos, pos + moveVector)
    end

    return Enum.ContextActionResult.Sink
end

-- Bind controls
CAS:BindAction(MOVE_ACTION, onMove, false,
    Enum.KeyCode.A, Enum.KeyCode.D, Enum.KeyCode.Left, Enum.KeyCode.Right)

CAS:BindAction(JUMP_ACTION, onJump, false, Enum.KeyCode.Space)
``` This is 67 lines instead of 91
#

I have no way to debug it due to not knowing any context to how it's used so I'll leave that up to you

#

logically it should function the same

obsidian quiver
#

I can make it lower and without breaking them but what I want is less than 35 lines by total lines

#

But thank you for help