isa_Khamdan Posted September 12, 2013 Share Posted September 12, 2013 Hello , I want to make a script that destroy cars if they stop in a collshape ( safe area ) for 5 seconds so if someone stopped his car and didn't move in 5 seconds the car will be destroyed. So I am going to use If isElementWithinCollsShape to check if the vehicle inside the safe area or not and then I will use getElementSpeed to see if the element is moving or not and if it's speed is 0 then it will destroy it So it's all good now but there is one thing that I didn't figure out how to do it How can I make it count the time so if player stopped his vehicle for 5 seconds then it will destroy it? Link to comment
Castillo Posted September 12, 2013 Share Posted September 12, 2013 setTimer destroyElement Link to comment
isa_Khamdan Posted September 13, 2013 Author Share Posted September 13, 2013 setTimer destroyElement Why it doesn't work? local Line1 = createColRectangle ( -1287.9871826172, -217.75384521484, 500, 500) function SafeZoneA(thePlayer) if getElementType ( thePlayer ) == "vehicle" then if (isElementWithinColShape(thePlayer, Line1)) then if getElementVelocity ( thePlayer == 0) then setTimer ( destroyElement , 10000, 1, thePlayer ) end end end end addEventHandler ( "onColShapeHit", getRootElement(), SafeZoneA ) Link to comment
Castillo Posted September 13, 2013 Share Posted September 13, 2013 if getElementVelocity ( thePlayer == 0) then That's wrong, change it to: if ( getElementVelocity ( thePlayer ) == 0 ) then Link to comment
isa_Khamdan Posted September 13, 2013 Author Share Posted September 13, 2013 if getElementVelocity ( thePlayer == 0) then That's wrong, change it to: if ( getElementVelocity ( thePlayer ) == 0 ) then it doesn't work? Link to comment
Castillo Posted September 13, 2013 Share Posted September 13, 2013 getElementVelocity returns 3 values: velocity X, velocitY and velocityZ. https://wiki.multitheftauto.com/wiki/GetElementSpeed You could try with that, remember to copy the function source code, as is not a native function. Link to comment
isa_Khamdan Posted September 13, 2013 Author Share Posted September 13, 2013 It doesn't work local Line1 = createColRectangle ( -1318.4671630859, -189.9077911377, 1000, 1000) function SafeZoneA(thePlayer) if getElementType ( thePlayer ) == "vehicle" then if (isElementWithinColShape(thePlayer, Line1)) then if ( getElementVelocity ( thePlayer ) == 0 ) then setTimer ( destroyElement , 1000, 1, thePlayer ) end end end end addEventHandler ( "onColShapeHit", getRootElement(), SafeZoneA ) 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 Link to comment
Castillo Posted September 13, 2013 Share Posted September 13, 2013 And where are you using the getElementSpeed function? you're still using getElementVelocity... Link to comment
isa_Khamdan Posted September 14, 2013 Author Share Posted September 14, 2013 And where are you using the getElementSpeed function? you're still using getElementVelocity... Still the same local Line1 = createColRectangle ( -1318.4671630859, -189.9077911377, 1000, 1000) function SafeZoneA(Vehicle) if getElementType ( Vehicle ) == "vehicle" then if (isElementWithinColShape(Vehicle, Line1)) then local Speed = getElementSpeed(Vehicle,"kmh") if Speed == 0 then setTimer ( destroyElement , 1000, 1, Vehicle ) end end end end addEventHandler ( "onColShapeHit", getRootElement(), SafeZoneA ) 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 Link to comment
Blaawee Posted September 14, 2013 Share Posted September 14, 2013 local Line1 = createColRectangle ( -1318.4671630859, -189.9077911377, 1000, 1000) function SafeZoneA( element ) if source == Line1 then if getElementType ( element ) == "vehicle" then if ( isElementWithinColShape( element, source ) ) then if getElementSpeed( element ) == 0 then setTimer ( destroyElement , 1000, 1, element ) end end end end end addEventHandler ( "onColShapeHit", resourceRoot, SafeZoneA ) 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 Link to comment
isa_Khamdan Posted September 14, 2013 Author Share Posted September 14, 2013 local Line1 = createColRectangle ( -1318.4671630859, -189.9077911377, 1000, 1000) function SafeZoneA( element ) if source == Line1 then if getElementType ( element ) == "vehicle" then if ( isElementWithinColShape( element, source ) ) then if getElementSpeed( element ) == 0 then setTimer ( destroyElement , 1000, 1, element ) end end end end end addEventHandler ( "onColShapeHit", resourceRoot, SafeZoneA ) 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 It doesn't destroy the vehicle and there are no errors in debug Edit: it only destroy the car if there was no one driving it Link to comment
Castillo Posted September 14, 2013 Share Posted September 14, 2013 Have you checked what getElementSpeed is returning? Link to comment
isa_Khamdan Posted September 14, 2013 Author Share Posted September 14, 2013 Have you checked what getElementSpeed is returning? No , How can I fix it? Link to comment
Castillo Posted September 14, 2013 Share Posted September 14, 2013 Before this line: if getElementSpeed( element ) == 0 then Add: outputChatBox ( tostring ( getElementSpeed ( element ) ) ) Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 Before this line: if getElementSpeed( element ) == 0 then Add: outputChatBox ( tostring ( getElementSpeed ( element ) ) ) It return the speed "142.87299415513" Link to comment
Castillo Posted September 15, 2013 Share Posted September 15, 2013 Which means that the vehicle will not be destroyed, because the speed has to be 0. Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 Which means that the vehicle will not be destroyed, because the speed has to be 0. I used a timer so if the vehicle stopped for 5 seconds it will get destroyed or what I did is wrong? Link to comment
Castillo Posted September 15, 2013 Share Posted September 15, 2013 No, because the timer is AFTER you check the speed, so the timer will never be created if the speed isn't 0. Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 No, because the timer is AFTER you check the speed, so the timer will never be created if the speed isn't 0. Hmm I forgot that So how can I fix it? Edit: will this work? Getting the speed first then check if the speed is 0 or not and if not then return? Link to comment
Castillo Posted September 15, 2013 Share Posted September 15, 2013 You can set the timer to execute a function which will check the vehicle speed, if it's 0, then destroy the vehicle. Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 You can set the timer to execute a function which will check the vehicle speed, if it's 0, then destroy the vehicle. How can I make a timer like this? I made the Function function Destroy( element ) local Speed = getElementSpeed( element ) if getElementType ( element ) == "vehicle" then if Speed == 0 then destroyElement( element ) end end end Link to comment
Castillo Posted September 15, 2013 Share Posted September 15, 2013 Yes, then you set the 10 seconds timer to execute "Destroy" function, and you have to pass "element" as an argument. Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 Yes, then you set the 10 seconds timer to execute "Destroy" function, and you have to pass "element" as an argument. This should work? local Line1 = createColRectangle ( -1318.4671630859, -189.9077911377, 1000, 1000) function Destroy( element ) local Speed = getElementSpeed( element ) if getElementType ( element ) == "vehicle" then if Speed == 0 then destroyElement( element ) end end end addEvent ( "Destroy", true ) addEventHandler ( "onColShapeHit", resourceRoot, Destroy ) function SafeZoneA( element ) if source == Line1 then if getElementType ( element ) == "vehicle" then if ( isElementWithinColShape( element, source ) ) then setTimer ( triggerEvent , 5000, 1, Destroy ) end end end end end addEventHandler ( "onColShapeHit", resourceRoot, SafeZoneA ) 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 Link to comment
Castillo Posted September 15, 2013 Share Posted September 15, 2013 No, that doesn't make any sense. Link to comment
isa_Khamdan Posted September 15, 2013 Author Share Posted September 15, 2013 No, that doesn't make any sense. I searched the Wiki but I didn't find anything about execute Function 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