I want to make a system in which whenever you jump, your left/right keys get temporarily disabled as instead you use a bodyforce to move your character.
The issue is, however, that when I try to sink the actions “CharacterLeft” and “CharacterRight”, they also do this bug where if you press forward and then jump, when landing you stop moving forward and have to repress the forward key. Despite this, it appears everything else works as intended.
I have currently tried not sinking the functions and instead the A and D keys, however that also creates the bug.
local STOP_MOVE_LEFT = "disableMovementLeft"
local STOP_MOVE_RIGHT = "disableMovementRight"
local leftMove = Enum.PlayerActions.CharacterLeft
local rightMove = Enum.PlayerActions.CharacterRight
local function leftMovement(_, inputState, _)
if inputState == Enum.UserInputState.Begin then
local leftVector = -root.CFrame.RightVector
while UIS:IsKeyDown(Enum.KeyCode.A) do
if grounded == true then break end
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = root
leftVector = -root.CFrame.RightVector
bodyForce.Force = leftVector * hum.WalkSpeed
Debris:AddItem(bodyForce, 0.25)
print(tostring(bodyForce.Force))
task.wait()
end
end
end
local function rightMovement(_, inputState, _)
if inputState == Enum.UserInputState.Begin then
local rightVector = root.CFrame.RightVector
while UIS:IsKeyDown(Enum.KeyCode.D) do
if grounded == true then break end
local bodyForce = Instance.new("BodyForce")
bodyForce.Parent = root
rightVector = root.CFrame.RightVector
bodyForce.Force = rightVector * hum.WalkSpeed
Debris:AddItem(bodyForce, 0.25)
print(tostring(bodyForce.Force))
task.wait()
end
end
end
local function stopMovement()
CAS:BindAction(
STOP_MOVE_LEFT,
function() return Enum.ContextActionResult.Sink end,
false,
leftMove
)
CAS:BindAction(
STOP_MOVE_RIGHT,
function() return Enum.ContextActionResult.Sink end,
false,
rightMove
)
CAS:BindAction(
"moveLeft",
leftMovement,
false,
leftMove
)
CAS:BindAction(
"moveRight",
rightMovement,
false,
rightMove
)
end
local function startMovement()
CAS:UnbindAction(STOP_MOVE_LEFT)
CAS:UnbindAction(STOP_MOVE_RIGHT)
CAS:UnbindAction("moveLeft")
CAS:UnbindAction("moveRight")
end
RS.RenderStepped:Connect(function()
spdText.Text = tostring(math.round(hum.WalkSpeed))
local origin = chr:WaitForChild("HumanoidRootPart").Position
local direction = Vector3.new(0, -1, 0) * 4
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {chr}
local result = workspace:Raycast(origin, direction, params)
if result then
hum.PlatformStand = false
grounded = true
startMovement()
else
grounded = false
stopMovement()
if hum.MoveDirection == Vector3.new(0,0,0) then
hum.PlatformStand = true
else
hum.PlatformStand = false
end
end
end)
Is this just because of a bug or is there a better way to do this?