OrbTanT Posted December 28, 2016 Share Posted December 28, 2016 I'm doing a medical kit system where the player has to stand still for 5 seconds and stay in the same position to be able to use the medical kit, even if I'm stopped without moving back I moved, tried to pack In various ways, what is the problem? function isElementMoving ( theElement ) if isElement ( theElement ) then local x, y, z = getElementVelocity( theElement ) return x ~= 0 or y ~= 0 or z ~= 0 end return false end function onPlayerTryUsinKit(player) if(player.type == "player") then isKit = player:getData("kit") if (isKit ~= false) then if not (isPedInVehicle(player)) then isMoving = isElementMoving(player) if (isMoving) then player:outputChat("Você precisa estar parado para usar o Kit Medico!", 255, 255, 0, false) else player:outputChat("Fique parado por 5 segundo para aplicar o Kit Medico", 255, 255, 0, false) posX, posY, posZ = player:getPosition() player:setData("dataX", posX) player:setData("dataY", posY) Timer(useKitMedic, 5000, 1, player) end end else player:outputChat("Você não tem Kit Medico para usar!", 255, 255, 0, false) end end end function useKitMedic(player) dataX = player:getData("dataX") dataY = player:getData("dataY") x, y, z = player:getPosition() if not(x == dataX) and (y == dataY) then player:outputChat("Falha ao usar o Kit Medico! Voce se moveu!", 255, 0, 0, false) else kitTimer = Timer(function(player) health = player:getHealth() player:setHealth(health + 5) end, 1000, 0, player) end end addEventHandler("onResourceStart", resourceRoot, function () local players = getElementsByType("player") for k,v in ipairs(players) do bindKey(v, "K", "down", onPlayerTryUsinKit) end end ) Link to comment
myonlake Posted December 28, 2016 Share Posted December 28, 2016 (edited) Well, the position was checked incorrectly, here is a better version with minimal changes done to the code: function Vector3:compare( comparison, precision ) if ( not precision ) then if ( self:getX( ) ~= comparison:getX( ) ) or ( self:getY( ) ~= comparison:getY( ) ) or ( self:getZ( ) ~= comparison:getZ( ) ) then return false end else if ( math.abs( self:getX( ) - comparison:getX( ) ) > precision ) or ( math.abs( self:getY( ) - comparison:getY( ) ) > precision ) or ( math.abs( self:getZ( ) - comparison:getZ( ) ) > precision ) then return false end end return true end function isElementMoving ( theElement ) if isElement ( theElement ) then local x, y, z = getElementVelocity( theElement ) return x ~= 0 or y ~= 0 or z ~= 0 end return false end function onPlayerTryUsinKit(player) if(player.type == "player") then isKit = player:getData("kit") if (isKit ~= false) then if not (isPedInVehicle(player)) then isMoving = isElementMoving(player) if (isMoving) then player:outputChat("Você precisa estar parado para usar o Kit Medico!", 255, 255, 0, false) else player:outputChat("Fique parado por 5 segundo para aplicar o Kit Medico", 255, 255, 0, false) player:setData("position", player:getPosition(),false) Timer(useKitMedic, 5000, 1, player) end end else player:outputChat("Você não tem Kit Medico para usar!", 255, 255, 0, false) end end end function useKitMedic(player) if(not player:getPosition():compare(player:getData('position'))) then player:outputChat("Falha ao usar o Kit Medico! Voce se moveu!", 255, 0, 0, false) else kitTimer = Timer(function(player) if(isElement(player)) then if(not player:getPosition():compare(player:getData('position'))) then player:outputChat("Falha ao usar o Kit Medico! Voce se moveu!", 255, 0, 0, false) kitTimer:destroy() else health = player:getHealth() player:setHealth(health + 5) player:outputChat('ok') end end end, 1000, 0, player) end end addEventHandler("onResourceStart", resourceRoot, function () local players = getElementsByType("player") for k,v in ipairs(players) do bindKey(v, "K", "down", onPlayerTryUsinKit) end end ) Here is a little bit more cleaned up version: --[[ Let's make a comparison method for Vector3 to make our job faster. bool Vector3:compare( Vector3 comparison [ , float precision ] ) ]] function Vector3:compare( comparison, precision ) -- If we're not using any specific precision, let's optimize -- the process and just check if they are not equal if ( not precision ) then if ( self:getX( ) ~= comparison:getX( ) ) or ( self:getY( ) ~= comparison:getY( ) ) or ( self:getZ( ) ~= comparison:getZ( ) ) then return false end else -- If we have some precision set, let's compare all absolute -- components to the precision if ( math.abs( self:getX( ) - comparison:getX( ) ) > precision ) or ( math.abs( self:getY( ) - comparison:getY( ) ) > precision ) or ( math.abs( self:getZ( ) - comparison:getZ( ) ) > precision ) then return false end end -- If all went fine, let's return the good news return true end --[[ Since MTA doesn't come with this method, let's make one. So we basically compare the velocity vector to the zeroed vector. bool Element:isMoving( void ) ]] function Element:isMoving( ) return not self:getVelocity( theElement ):compare( Vector3( 0, 0, 0 ) ) end --[[ Let's initialize a method for the player that starts the useKit process. void Player:tryKit( void ) ]] function Player:tryKit( ) -- If we're dead, we can't really do anything anyway. if ( self:isDead( ) ) then return end -- And let's make sure we actually need that medkit. if ( self:getHealth( ) < 100 ) then -- How many kits are left to use? local kits = self:getData( 'kit' ) -- If we have any kits available. if ( kits ) and ( tonumber( kits ) > 0 ) then -- And we're not in a car, because we need to pay attention to the road, of course (no Teslas in 90's). if ( not self:isInVehicle( ) ) then -- Let's make sure we're not moving. if ( self:isMoving( ) ) then self:outputChat( "Você precisa estar parado para usar o Kit Medico!", 255, 0, 0 ) else -- Once we're past all the checks, let's save our position (no-sync) and initialize the kit timer for 5000 ms self:outputChat( "Fique parado por 5 segundo para aplicar o Kit Medico", 255, 255, 0 ) self:setData( 'position', self:getPosition( ), false ) self.kitTimer = Timer( function( player ) -- Once that timer is past, we'll start healing ourselves! player:useKit( ) end, 5000, 1, self ) end end else self:outputChat( "Você não tem Kit Medico para usar!", 255, 0, 0 ) end else self:outputChat( "You already have max health.", 255, 0, 0 ) end end --[[ When we want to use a kit, we'll call this method. void Player:useKit( void ) ]] function Player:useKit( ) -- If we're dead, we can't really do anything anyway. if ( self:isDead( ) ) then return end -- Have we moved from our saved position? if ( not self:getPosition( ):compare( self:getData( 'position' ) ) ) then self:outputChat( "Falha ao usar o Kit Medico! Voce se moveu!", 255, 0, 0 ) else -- Let's get our current health. local health = self:getHealth( ) -- And let's make sure we actually need a fix. if ( health < 100 ) then -- Yep, let's hit that arm with some of that good stuff. self:setHealth( health + 5 ) -- Is there any need for more? if ( health + 5 < 100 ) then -- Sure there is, so let's keep on doing that every 1000 ms. self.kitTimer = Timer( function( player ) -- Eat, sleep, heal, repeat... or what was that again. player:useKit( ) end, 1000, 1, self ) end end end end --[[ Funny thing is as much as healing yourself while not on the server sounds good, we're not able to do that on the fly, so we'll just stop timers when you quit. ]] addEventHandler( 'onPlayerQuit', root, function( ) -- If there is a kitTimer initialized, we'll destroy it! if ( isTimer( source.kitTimer ) ) then source.kitTimer:destroy( ) end end ) --[[ When the party begins... ]] addEventHandler( 'onResourceStart', resourceRoot, function( ) -- We start by inviting all of our friends over. for _, player in ipairs( Element.getAllByType( 'player' ) ) do -- And then giving them some of those medkit juice drinks that everybody loves. bindKey( player, 'K', 'down', function( ) -- And so they can now taste them by pressing that good ol' K. Sound good? player:tryKit( ) end ) end end ) Edited December 28, 2016 by myonlake added comments Link to comment
OrbTanT Posted December 28, 2016 Author Share Posted December 28, 2016 Really thanks for the help Link to comment
myonlake Posted December 28, 2016 Share Posted December 28, 2016 1 minute ago, Shinigami said: Really thanks for the help You're welcome. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now