GetSpeed() not working?
#5
Since nobody is answering me, I decided to make an extra class for the GetSpeed() function that stores the current position and the position a second ago. So, for everyone having the same problem as me; here is the class.
require "classes.class"

rawPlayer = class(
  function(self, uuid, currPosX, currPosY, currPosZ)
    self.uuid = uuid
    self.currPosX = currPosX
    self.currPosY = currPosY
    self.currPosZ = currPosZ
    self.lastTime = os.clock()
  end
)

function rawPlayer:setPos(posX, posY, posZ)
  if lastTime < os.clock then
    self.lastPosX = self.currPosX
    self.lastPosY = self.currPosY
    self.lastPosZ = self.currPosZ
    
    self.currPosX = posX
    self.currPosY = posY
    self.currPosZ = posZ
  end
end

function rawPlayer:GetSpeedX()
  speedX = self.currPosX - self.lastPosX
  
  if speedX < 0 then
    speedX = speedX * (-1)
  end

  return speedX
end
  
function rawPlayer:GetSpeedY()
  speedY = self.currPosY - self.lastPosY
  
  if speedY < 0 then
    speedY = speedY * (-1)
    end
  
  return speedY
end
  
  function rawPlayer:GetSpeedZ()
  speedZ = self.currPosZ - self.lastPosZ
  
  if speedZ < 0 then
    speedZ = speedZ * (-1)
  end
  
  return speedZ
end

and the 'class()' (skidded from here)

-- class.lua
-- Compatible with Lua 5.1 (not 5.0).
function class(base, init)
   local c = {}    -- a new class instance
   if not init and type(base) == "function" then
      init = base
      base = nil
   elseif type(base) == "table" then
    -- our new class is a shallow copy of the base class!
      for i,v in pairs(base) do
         c[i] = v
      end
      c._base = base
   end
   -- the class will be the metatable for all its objects,
   -- and they will look up their methods in it.
   c.__index = c

   -- expose a constructor which can be called by <classname>(<args>)
   local mt = {}
   mt.__call = function(class_tbl, ...)
   local obj = {}
   setmetatable(obj,c)
   if init then
      init(obj,...)
   else 
      -- make sure that any stuff from the base class is initialized!
      if base and base.init then
      base.init(obj, ...)
      end
   end
   return obj
   end
   c.init = init
   c.is_a = function(self, klass)
      local m = getmetatable(self)
      while m do 
         if m == klass then return true end
         m = m._base
      end
      return false
   end
   setmetatable(c, mt)
   return c
end
Reply
Thanks given by:


Messages In This Thread
GetSpeed() not working? - by G3bE - 08-20-2018, 01:43 AM
RE: GetSpeed() not working? - by G3bE - 08-20-2018, 01:44 AM
RE: GetSpeed() not working? - by NiLSPACE - 08-20-2018, 04:41 AM
RE: GetSpeed() not working? - by G3bE - 08-20-2018, 07:06 AM
RE: GetSpeed() not working? - by G3bE - 09-10-2018, 06:33 AM
RE: GetSpeed() not working? - by NiLSPACE - 09-10-2018, 10:13 PM
RE: GetSpeed() not working? - by G3bE - 09-13-2018, 07:27 PM
RE: GetSpeed() not working? - by NiLSPACE - 09-14-2018, 12:41 AM
RE: GetSpeed() not working? - by G3bE - 09-14-2018, 02:03 AM
RE: GetSpeed() not working? - by NiLSPACE - 09-14-2018, 04:04 AM
RE: GetSpeed() not working? - by G3bE - 09-21-2018, 06:21 AM
RE: GetSpeed() not working? - by JARROD - 07-25-2019, 07:04 PM
RE: GetSpeed() not working? - by Krystilize - 03-07-2020, 11:00 AM



Users browsing this thread: 2 Guest(s)