Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making player always face left or right?

Asked by 2 years ago

I'm making a 2d game and I've been trying to find a way for the player to always face left or right depending on their inputs, I already have a script for this but it doesn't work at all and I can't figure out why. I couldn't find much on this topic on the devforums so I'm coming here for help, here's my script

I also already have a 2d camera that follows the player around, so I tried getting it's left and right vectors to put on the humanoid part

local players = game.Players
local player = players.LocalPlayer
local character = player.Character
local root = character:WaitForChild"HumanoidRootPart"
local camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")

local keyA = false
local keyD = false

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.Enum.KeyCode.D then
        keyD = true
    elseif InputObject.UserInputType == Enum.UserInputType.Enum.KeyCode.A then
        keyA = true
    end
end)

UserInputService.InputEnded:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.Enum.KeyCode.D then
        keyD = false
    elseif InputObject.UserInputType == Enum.UserInputType.Enum.KeyCode.A then
        keyA = false
    end
end)

spawn(function()
    while true do wait ()
        if keyA == true and keyD == false then
            root.CFrame = CFrame.new(root.Position, root.Position - camera.CFrame.RightVector)
        elseif keyA == false and keyD == true then
            root.CFrame = CFrame.new(root.Position, root.Position + camera.CFrame.RightVector)
        end
    end
end)

Can anyone smarter than me (which is everyone) help me on why it isn't working, or a better script to do the same thing? Thanks !

Answer this question