Help with RunService and metatables

I’ve created a custom movement script and I’m now trying to combine it with Roblox’s own movement scripts as I would assume they are much more efficient.

I’ve run into a bit of a problem with this section, as for some reason it returns an error I can’t figure out.

Roblox’s control system uses metatables, which I’m not really familiar with at all. So far I’ve made everything work by reading up on it on the DevHub, but I still don’t fully understand it. I’ve just ran into this issue where by trying to use RunService on a function, it fails for some reason.
It produces this error:

Expected ‘(’, ‘{’ or when parsing function call, got ‘)’

Here’s the function:

function Keyboard:UpdateMovement(InputState)
	
	if not HumanoidRootPart then return end
	
	if InputState == Enum.UserInputState.Cancel then
		
		self.MoveVector = PlayerVelocity
		
	else
		
		self.MoveVector = Vector3.new(self.LeftValue + self.RightValue, 0, self.FwdValue + self.BackValue)
		
	end
	
	local NewDirection = Vector3.new()

	if PlayerVelocity ~= Vector3.new(0, 0, 0) then 

		NewDirection = PlayerVelocity.Unit 

	end

	local NewSpeed = PlayerVelocity.Magnitude

	Humanoid.WalkSpeed = NewSpeed
	Humanoid:Move(NewDirection, false)
	
end

RunService.RenderStepped:Connect(self:UpdateMovement)
-- This is where the error occurs

Any help is appreciated. Thanks!

Greetings.
Try to put some brackets after “UpdateMovement” like this: RunService.RenderStepped:Connect(self:UpdateMovement())

This did do something, however now it returns a different error: attempt to index nil with “UpdateMovement”

Probably you need an arg inside the brackets since in the function it’s UpdateMovement(InputState)

RunService.RenderStepped:Connect(function()
self:UpdateMovement()
end)

Unfortunately no, this still returns the same error only this time it repeats every few milliseconds or so

what about removing the “self” before UpdateMovement()?
RunService.RenderStepped:Connect(UpdateMovement())

self isn’t defined here. Also, you can’t call these type of functions directly from the event, you’ll have to use another function.

RunService.RenderStepped:Connect(function()
    Keyboard:UpdateMovement()
end)

doesn’t seem that you identify self anywhere in this code. You could do

local self = setmetatable({}, Keyboard)

or you could do

Keyboard:UpdateMovement()

instead

You have to put the connection code within the object creation function otherwise self isn’t going to exist in that scope. Can you show me the whole code, if you are confused?

I believe the issue here is that he is trying to integrate Roblox’s code for the movement scripts with his own without fully understanding how their code works. Which is that their code works by separating the controllers for each type of input into their own custom class.

In order to properly integrate their code, he would need to create a new instance of Keyboard and then use that, for example:

local Keyboard = require(path.to.keyboard_module)

local newController = Keyboard.new(Enum.ContextActionPriority.Default.Value)

RunService.RenderStepped:Connect(function()
    newController:UpdateMovement()
end)

That script there does work (well, at least it removes the errors - I still can’t move my character though). I’m pretty sure what I was having the issue with is resolved but I will need to go back over my other imported code to fix this. I’ll have to learn more about metatables and carefully read over every line of Roblox’s code first though to make sure I do it right. I underestimated how complex it would be - it’s definitely advanced whereas I’m only intermediate at best.

Thank you everyone for your help, I know I didn’t reply to everyone but the thread was moving pretty fast and I had a lot of possible solutions to try - just know I did attempt everything as best as I could in the short time I had to try it.