Tips over custom movement

Hi, i made a custom movement and i’m wondering if i can get tips over the script so
that next time i can improve upon it.

here’s module

local Vector3 = {}
Vector3.__index = Vector3

---------- DEFAULTS ----------

local default = {}

default.speed = 5
default.acceleration = 5000
default.maxSpeed = 20

-- vector3

function Vector3.New(x, y ,z)
	local self = setmetatable({}, Vector3)

	self.x = x or 0
	self.y = y or 0
	self.z = z or 0

	return self
end

function Vector3.moveFoward(self, userInputState)
	if (userInputState == Enum.UserInputState.Begin) then
		self.x = default.speed * default.acceleration
	else
		if (userInputState == Enum.UserInputState.End) then
			self.x = 0
		end
	end
end

function Vector3.moveBackwards(self, userInputState)
	if (userInputState == Enum.UserInputState.Begin) then
		self.x = -default.speed * default.acceleration
	else
		if (userInputState == Enum.UserInputState.End) then
			self.x = 0
		end
	end
end

function Vector3.moveLeft(self, userInputState)
	if (userInputState == Enum.UserInputState.Begin) then
		self.z = -default.speed * default.acceleration
	else
		if (userInputState == Enum.UserInputState.End) then
			self.z = 0
		end
	end
end

function Vector3.moveRight(self, userInputState)
	if (userInputState == Enum.UserInputState.Begin) then
		self.z = default.speed * default.acceleration
	else
		if (userInputState == Enum.UserInputState.End) then
			self.z = 0
		end
	end
end

return Vector3

here’s the local script

local contextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")

local replicatedStorage = game:GetService("ReplicatedStorage")
local vector3 = require(replicatedStorage:WaitForChild("Vector3"))

local rootPart = script.Parent:WaitForChild("RootPart")
local camera = game.Workspace:WaitForChild("Camera")

-- attachment

local attachment = Instance.new("Attachment")
attachment.Parent = rootPart

-- force

local force = Instance.new("VectorForce")
force.Attachment0 = attachment
force.Parent = rootPart

local self = vector3.New()

local function onFoward(actionName, userInputState, input)
	vector3.moveFoward(self, userInputState)
end

local function onBackwards(actionName, userInputState, input)
	vector3.moveBackwards(self, userInputState)
end

local function onLeft(actionName, userInputState, input)
	vector3.moveLeft(self, userInputState)
end

local function onRight(actionName, userInputState, input)
	vector3.moveRight(self, userInputState)
end

contextActionService:BindAction("forward", onFoward, false, Enum.KeyCode.W)
contextActionService:BindAction("backwards", onBackwards, false, Enum.KeyCode.S)
contextActionService:BindAction("left", onLeft, false, Enum.KeyCode.A)
contextActionService:BindAction("right", onRight, false, Enum.KeyCode.D)

runService.RenderStepped:Connect(function()
	local vector3 = Vector3.new(self.x, 0, self.z)
	force.Force = vector3
end)

This belongs in #help-and-feedback:code-review
I think these separate functions could be combined into one function, or at least use the same function and these two be a wrapper. You are essentially doing the same thing but negative on one function.

I am also confused, why is Z left and right? I thought that was X.

thanks for the tip, i was a bit confused over the z axis and the x axis and that’s why it went in a other direction then it’s supposed to be

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