So I got 2 scripts, one for the W and S movement. To the front and to the back.
And I got a second script for A and D movement, left and right.
Script 1 works with the rotation of the player, and the rotation of the player is pointed at the player’s mouse. The second script is for left and right, this is just left or right, this works with the camera (camera is locked)
The problem is, only one of them works when both of them are enabled. They both work as intended, but not together.
My question is, how do I “merge” these scripts into one, or, how do I make them both work when they’re both enabled?
Script for W and S movement + video
https://gyazo.com/ac802f3df097cd68c56bd7c48c58655a
local RunS = game:GetService("RunService")
local InputS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
player.CharacterAdded:Connect(function(_character)
character = _character
humanoidRootPart = _character:WaitForChild("HumanoidRootPart")
end)
local walkKeyBinds = {
Forward = { Key = Enum.KeyCode.W, Direction = Enum.NormalId.Front },
Backward = { Key = Enum.KeyCode.S, Direction = Enum.NormalId.Back }
}
local function getWalkDirectionCameraSpace()
local walkDir = Vector3.new()
for keyBindName, keyBind in pairs(walkKeyBinds) do
if InputS:IsKeyDown(keyBind.Key) then
walkDir += Vector3.FromNormalId( keyBind.Direction )
end
end
if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
end
return walkDir
end
local function getWalkDirectionWorldSpace()
local walkDir = humanoidRootPart.CFrame:VectorToWorldSpace( getWalkDirectionCameraSpace() )
walkDir *= Vector3.new(1, 0, 1) --Set Y axis to 0
if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
end
return walkDir
end
local function updateMovement( dt )
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move( getWalkDirectionWorldSpace() )
end
end
RunS.RenderStepped:Connect(updateMovement)
Script for A and D movement + video
https://gyazo.com/67e43a2dda721064a308614410813a7b
local RunS = game:GetService("RunService")
local InputS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
player.CharacterAdded:Connect(function(_character)
character = _character
humanoidRootPart = _character:WaitForChild("HumanoidRootPart")
end)
local function getWalkDirectionCameraSpace()
local walkKeyBinds = {
Left = { Key = Enum.KeyCode.A, Direction = Enum.NormalId.Left },
Right = { Key = Enum.KeyCode.D, Direction = Enum.NormalId.Right }
}
local walkDir = Vector3.new()
for keyBindName, keyBind in pairs(walkKeyBinds) do
if InputS:IsKeyDown(keyBind.Key) then
--print(walkKeyBinds)
walkDir += Vector3.FromNormalId( keyBind.Direction )
end
end
if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
end
return walkDir
end
local function getWalkDirectionWorldSpace()
local walkDir = camera.CFrame:VectorToWorldSpace( getWalkDirectionCameraSpace() )
walkDir *= Vector3.new(1, 0, 1) --Set Y axis to 0
if walkDir.Magnitude > 0 then --(0, 0, 0).Unit = NaN, do not want
walkDir = walkDir.Unit --Normalize, because we (probably) changed an Axis so it's no longer a unit vector
end
return walkDir
end
local function updateMovement( dt )
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid:Move( getWalkDirectionWorldSpace() )
end
end
RunS.RenderStepped:Connect(updateMovement)