Very early progress for movement controller

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
function calculations.facing()
	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

I know you stated that this isn’t important to the main issue, but I’m a bit confused why you’re breaking it up into separate vectors instead of returning M_common.cam.CFrame.LookVector? The LookVector should already be the direction that the camera is facing.

if keys[ZPK] and not keys[ZNK] then

To address the main issue here, you’re indexing keys with ZPK, ZNK, etc. as if it were a dictionary, but in the local script, you treat keys (pressedKeys) as an array. i.e., keys has the following format:

{
    Enum.KeyCode.Space,
    Enum.KeyCode.W,
    Enum.KeyCode.D,
    -- ...
}

However, calculations.inputDirection treats it as if the format was like this:

{
    [Enum.KeyCode.Space] = true,
    [Enum.KeyCode.W] = true,
    [Enum.KeyCode.D] = true
}

which is not the case.

1 Like

that takes out the y value and then normalizes the result. thank you for explaining the format stuff! i will try to work around it.

1 Like

Ahh okay I understand what you’re doing with those values now. Good luck :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.