Karuzo Posted February 11, 2014 Posted February 11, 2014 Hey Guys, i have a small problem with my Speed-Limiter Script. curMaxSpeed = false function limit_func ( cmd, amount ) local amount = tonumber ( amount ) if amount then local amount = math.abs ( amount ) curMaxSpeed = amount --curMaxSpeed = curMaxSpeed*0.00464 if not isTimer ( curMaxSpeedTimer ) then curMaxSpeedTimer = setTimer ( fixSpeed, 50, -1 ) end outputChatBox ( "Maximale Geschwindigkeit auf "..amount.." Km/h gesetzt. Verwende /stoplimit, um den Tempomat zu entfernen.", 150, 150, 0 ) else outputChatBox ( "Bitte gib eine gültige Km/h Zahl an!", 125, 0, 0 ) end end addCommandHandler ( "limit", limit_func ) function stoplimit_func () if not curMaxSpeed then outputChatBox ( "Du hast momentan den Tempomat nicht aktiviert. Verwende /limit [Km/h], um ihn zu aktivieren!", 150, 0, 0 ) else curMaxSpeed = false killTimer ( curMaxSpeedTimer ) outputChatBox ( "Tempomat wurde entfernt!", 0, 150, 0 ) end end addCommandHandler ( "stoplimit", stoplimit_func ) function fixSpeed () local veh = getPedOccupiedVehicle(lp) if veh then if isVehicleOnGround ( veh ) and getPedOccupiedVehicleSeat ( lp ) == 0 then local vx, vy, vz = getElementVelocity(veh) local speed = math.sqrt(vx^2 + vy^2 + vz^2) local kmh = speed * 180 if kmh > curMaxSpeed then setElementVelocity ( veh, vx*0.97, vy*0.97, vz*0.97 ) end end end end It outputs the message that the limit was set, but the caar doesn't really limit at that speed. No errors.
Bonsai Posted February 11, 2014 Posted February 11, 2014 Where is fixSpeed called? I suggest you using getElementSpeed and setElementSpeed, thats easier too handle. https://wiki.multitheftauto.com/wiki/Useful_Functions
Saml1er Posted February 12, 2014 Posted February 12, 2014 -- client sided local spd = "" function getElementSpeed(element,unit) if (unit == nil) then unit = 0 end if (isElement(element)) then local x,y,z = getElementVelocity(element) if (unit=="mph" or unit==1 or unit =='1') then return (x^2 + y^2 + z^2) ^ 0.5 * 100 else return (x^2 + y^2 + z^2) ^ 0.5 * 1.8 * 100 end else outputDebugString("Not an element. Can't get speed") return false end end function setElementSpeed(element, unit, speed) -- only work if element is moving! if (unit == nil) then unit = 0 end if (speed == nil) then speed = 0 end speed = tonumber(speed) local acSpeed = getElementSpeed(element, unit) if (acSpeed~=false) then -- if true - element is valid, no need to check again local diff = speed/acSpeed local x,y,z = getElementVelocity(element) setElementVelocity(element,x*diff,y*diff,z*diff) return true end return false end function speeder () local theV = getPedOccupiedVehicle ( localPlayer ) if tonumber ( getElementSpeed ( theV, "kph" ) ) >= spd then setElementSpeed (theV, "kph", spd ) end end function limit_func ( command, speedy ) local speedy = tonumber ( speedy ) if speedy then spd = speedy addEventHandler ("onClientRender", root, speeder ) end end addCommandHandler ( "limit", limit_func ) function stoplimit_func () removeEventHandler ("onClientRender", root, speeder ) end addCommandHandler ( "stoplimit", stoplimit_func )
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