GetSpeed() not working?
#1
I tried to print out the users speed with both GetSpeed().x/y/z and GetSpeedX/Y/Z() and it just send me nulls. Is there anything I should know for using this function? Huh Here is my onPlayerMoving() code:

function OnPlayerMoving(Player)
 Player:SendMessageInfo(\'Speed X:\' .. Player:GetSpeed().x)
end

The hook is the same as in "Writing a Cuberite plugin" in the registering hook part.(link postet as a reply)
Sincerely G3bE
Reply
Thanks given by:
#2
Link to the hook: https://api.cuberite.org/Writing-a-Cuberite-plugin.html
Reply
Thanks given by:
#3
Does it send 'nil' values or the actual number 0?
Reply
Thanks given by:
#4
(08-20-2018, 04:41 AM)NiLSPACE Wrote: Does it send 'nil' values or the actual number 0?

In the code above it's sending me the number 0, but in this code (yes, it is an anti cheat and I know that somebody else is doing this already)

function speedCheck(Player) -- called in OnPlayerMoving
  -- Horizontal speed check(doesn\'t include swimming)
 speed = Player:GetSpeedX() + Player:GetSpeedZ()
 -- if the player is sprinting
 if Player:IsSprinting() ~= nil then
   -- if he is faster than the max sprinting speed
   if speed > Player:GetSprintingMaxSpeed() then
     -- stop his movement
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 -- if the player is flying
 elseif Player:IsFlying() ~= nil then
   -- if he is faster than the max flying speed
   if speed > Player:GetFlyingMaxSpeed() then
     -- stop his movement
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 -- if the player is not sprinting and not flying he is walking normally
 else
   -- if he is faster than the max normal speed
   if speed > Player:GetNormalMaxSpeed() then
     -- stop his movement
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 end-- Horizontal speed check(doesn\'t includes swimming)
 speed = Player:GetSpeedX() + Player:GetSpeedY()
 -- if the player is sprinting
 if Player:IsSprinting() then
   -- if he is faster than the max sprinting speed
   if speed > Player:GetSprintingMaxSpeed() then
     -- stop his movement
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 -- if the player is flying
 elseif Player:IsFlying()  then
   -- if he is faster than the max flying speed
   if speed > Player:GetFlyingMaxSpeed() then
     -- stop his movement
     return true
   elseif Player:CanFly() == false then
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 -- if the player is not sprinting and not flying he is walking normally
 else
   -- if he is faster than the max normal speed
   if speed > Player:GetNormalMaxSpeed() then
     -- stop his movement
     return true
   -- if not
   else
     -- let the player move
     return false
   end
 end
end

it is sending me this error:

Code:
Warning: [23:03:44] LUA: Plugins/RAW_ac/checks/movement.lua:5: attempt to call method 'GetSpeedX' (a nil value)
Warning: [23:03:44] Stack trace:
Warning: [23:03:44]   Plugins/RAW_ac/checks/movement.lua(5): (no name)
Warning: [23:03:44]   (tail call)(-1):
Warning: [23:03:44] Stack trace end
Warning: [23:03:44] Error in <attached> calling function <callback>()
and the same comes for IsSprinting() if I set speed to 1 manually.
Hope that this helps.

Sincerely G3bE
Reply
Thanks given by:
#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:
#6
Sorry, I completely forgot to respond back. Next time you can bump the thread when nobody responds.

Does cPlayer:GetSpeed() work? I believe we had a change not too long ago where most XYZ functions were replaced with a single method that accepts a Vector. Perhaps something went wrong while trying to remain compatible with the XYZ methods.
Reply
Thanks given by:
#7
(09-10-2018, 10:13 PM)NiLSPACE Wrote: Sorry, I completely forgot to respond back. Next time you can bump the thread when nobody responds.

Does cPlayer:GetSpeed() work? I believe we had a change not too long ago where most XYZ functions were replaced with a single method that accepts a Vector. Perhaps something went wrong while trying to remain compatible with the XYZ methods.

no it is still not working Undecided
Reply
Thanks given by:
#8
But what does it return?
Reply
Thanks given by:
#9
(09-14-2018, 12:41 AM)NiLSPACE Wrote: But what does it return?

It returns a 0, but in a more advanced code like my first answer says it gives me an error.
Reply
Thanks given by:
#10
That's really strange as GetSpeed isn't supposed to return a number at all. It should return a Vector3d which contains 3 numbers (x, y, z). Do you have the complete codebase somewhere online so I could try it?
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)