I want to make basic a player sprint mechanic using OOP.
The issue is that metatable objects return nil when i try to call a method called “playerSprint”
I’ve been trying to find some code mistakes. I was comparing my code with OOP code examples, but I still can’t find the thing that causes this error.
Did I do something wrong?
Ok here’s a code.
local movementScript = {}
movementScript.__index = movementScript
local UIS = game:GetService("UserInputService")
local ReplicatedS = game:GetService("ReplicatedStorage")
function movementScript.new(player: Player)
local self = setmetatable({}, movementScript)
self.Player = player
self.Character = self.Player.Character
self.Humanoid = self.Character.Humanoid
self.boundKeys = require(ReplicatedS.Config.BoundKeys)
return self
end
function movementScript:playerSprint(input: InputObject, isSprint: boolean)
if table.find(self.boundKeys.Sprint, input.KeyCode) then
if isSprint then
self.Humanoid.WalkSpeed = self.Character:GetAttribute("sprintSpeed")
else
self.Humanoid.WalkSpeed = self.Character:GetAttribute("walkSpeed")
end
end
end
UIS.InputBegan:Connect(function(input)
movementScript:playerSprint(input, true)
end)
UIS.InputEnded:Connect(function(input)
movementScript:playerSprint(input, false)
end)
return movementScript