hi i want to preface with the fact that im kinda new to scripting and dont know many conventions for things so sorry if the code is hard to look at. i am working on a movement controller and i want to be able to detect the button presses but the system i made isnt working…
you can probly ignore most of the local script stuff also this is my first time using module scripts
this is the local script:
local modules = game.StarterPlayer.StarterPlayerScripts
--modules
local M_inputPlus = require(modules.InputPlus)
local M_common = require(modules.Common)
local M_movement = require(modules.Movement)
--service
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
--config
local walkSpeed = 35 --VVV these are the keys VVV
local forwardKey = Enum.KeyCode.W
local backwardKey = Enum.KeyCode.S
local rightKey = Enum.KeyCode.D
local leftKey = Enum.KeyCode.A
--locals
local inputDir = Vector3.new()
local pressedKeys = {}
--code
UIS.InputBegan:Connect(function(input) --this prints without the "enum.keycode" for some reason
if input.UserInputType == Enum.UserInputType.Keyboard then
table.insert(pressedKeys, input.KeyCode)
end
end)
UIS.InputEnded:Connect(function(input)
for i, key in ipairs(pressedKeys) do
if key == input.KeyCode then
table.remove(pressedKeys, i)
break
end
end
end)
RS.RenderStepped:Connect(function() --keys, ZPK, ZNK, XPK, XNK vvvvv
inputDir = M_movement.inputDirection(pressedKeys, forwardKey, backwardKey, rightKey, leftKey)
if inputDir.Magnitude > 0 then
M_common.HRP.AssemblyLinearVelocity = inputDir * walkSpeed
end
end)
this is the module script:
local M_common = require(game.StarterPlayer.StarterPlayerScripts.Common)
local calculations = { }
function calculations.facing() --this just gets the direction the camera is looking so its not too important to the main issue
local camZ = M_common.cam.CFrame.LookVector
local camX = M_common.cam.CFrame.RightVector
local ZFace = Vector3.new(camZ.X, 0, camZ.Z).Unit
local XFace = Vector3.new(camX.X, 0, camX.Z).Unit
return ZFace, XFace
end
function calculations.inputDirection(keys,ZPK,ZNK,XPK,XNK) --z positve, z negative, ect...
local ZDir = Vector3.new()
local XDir = Vector3.new()
local ZFace, XFace = calculations.facing()
if keys[ZPK] and not keys[ZNK] then -- i think the problem is matching it to the table like: keys[keyhere]
ZDir = ZFace
elseif keys[ZNK] and not keys[ZPK] then
ZDir = -ZFace
end
if keys[XPK] and not keys[XNK] then
XDir = XFace
elseif keys[XNK] and not keys[XPK] then
XDir = -XFace
end
local sumDir = ZDir + XDir
local inputDir = sumDir.Unit
return inputDir
end
return calculations