BinSlayer1 Posted August 1, 2010 Share Posted August 1, 2010 Hi! I'm a beginner ( fine, call me noob ) at scripting in LUA. I'm trying to make a script for a race server. The script is supposed to make the afk-players have ghostmode on start of maps. I thought the script through as this: GM gets enabled on every map-start. When the client presses "w" or "s", their ghostmode gets turned off. This is what I managed to get and it doesn't give any errors, but it doesn't work either, so if anyone could help me correct it I would appreciate it. addEvent('onClientMapStarting', true) function GhostMode() thePlayer = source setElementData( thePlayer, "overrideCollide.uniqueblah", 0, false ) if getKeyState( "w" ) == true or getKeyState( "s" ) == true then setElementData( thePlayer, "overrideCollide.uniqueblah", nil, false ) end end addEventHandler('onClientMapStarting', getRootElement(), GhostMode) Link to comment
dzek (varez) Posted August 1, 2010 Share Posted August 1, 2010 lets check it line by line, or something: (btw, use "lua" tags, not "code") addEvent('onClientMapStarting', true) -- we are adding event function GhostMode() -- this is fired when map is starting thePlayer = source setElementData( thePlayer, "overrideCollide.uniqueblah", 0, false ) -- we are setting element data if getKeyState( "w" ) == true or getKeyState( "s" ) == true then -- and ONCE (function is fired once! it's logical)on this one frame when event is being fired, we are checking if player is pressing w or s.. so player need to keep it pressed all the time on new map start to make it work.. setElementData( thePlayer, "overrideCollide.uniqueblah", nil, false ) end end addEventHandler('onClientMapStarting', getRootElement(), GhostMode) -- we are adding event handler. once again: THIS IS FIRED ONCE on every map start you should add timer (and remove old timer, if player was afk whole round) checking if player is pressing ACCELERATION or BRAKE/REVERSE (not "w", "s".. im playing using key up/down arrow, somebody could use "i" "k", somebody even "f3" and "f10" ;p -- use getControlState) on this timer after player is pressing needed key - remove the timer and setElementData Link to comment
BinSlayer1 Posted August 1, 2010 Author Share Posted August 1, 2010 I understand that I need to use getControlState (it makes sense), but I don't really know how to use a timer can you give me an example or something? or even for this one. I learn easier when I see the code. Link to comment
dzek (varez) Posted August 1, 2010 Share Posted August 1, 2010 ghostM = false -- default, on join - its false (changing that will not cause ghost mode to be on btw) AFKtimer = nil function checkAFK() if (conditions) -- replace conditions with getControlState setElementData( thePlayer, "overrideCollide.uniqueblah", nil, false) removeTimer(AFKtimer) end end function GhostMode() thePlayer = source setElementData( thePlayer, "overrideCollide.uniqueblah", 0, false ) if isTimer(AFKtimer) then killTimer(AFKtimer) end -- we kill timer if theres already one AFKtimer = setTimer(checkAFK, 50, 0) -- we are creating new one end i wrote it in browser, could not work, but should.. try some debugging if not (put outputDebugString and some text after each function, if, etc - see debug with "debugscript 3" in ingame console) Link to comment
BinSlayer1 Posted August 1, 2010 Author Share Posted August 1, 2010 Hey, thanks for the fast reply ghostM = false AFKtimer = nil function checkAFK() thePlayer = source if ( getControlState ( thePlayer, accelerate ) ) or ( getControlState ( thePlayer, brake_reverse ) ) then setElementData( thePlayer, "overrideCollide.uniqueblah", nil, false) removeTimer(AFKtimer) end end function GhostMode() thePlayer = source setElementData( thePlayer, "overrideCollide.uniqueblah", 0, false ) if isTimer(AFKtimer) then killTimer(AFKtimer) end AFKtimer = setTimer(checkAFK, 50, 0) end This is what I have. It doesn't give any errors in debugging, but it isn't working either. I'm not sure what the problem is but I'll try to see if it works with events. Link to comment
dzek (varez) Posted August 2, 2010 Share Posted August 2, 2010 2nd argument of getControlState is STRING getControlState(thePlayer, "accelerate") Link to comment
BinSlayer1 Posted August 2, 2010 Author Share Posted August 2, 2010 I have this in my gm.lua: ghostM = false AFKtimer = nil function checkAFK() thePlayer = source if ( getControlState ( thePlayer, "accelerate" ) ) or ( getControlState ( thePlayer, "brake_reverse" ) ) then setElementData( thePlayer, "overrideCollide.uniqueblah", nil, false ) removeTimer(AFKtimer) end end function GhostMode() thePlayer = source setElementData( thePlayer, "overrideCollide.uniqueblah", 0, false ) if isTimer(AFKtimer) then killTimer(AFKtimer) end AFKtimer = setTimer(checkAFK, 50, 0) end And I have this in my meta.xml: <meta> <script src="gm.lua" type="server" /> </meta> I'm afraid it still doesn't work Link to comment
50p Posted August 2, 2010 Share Posted August 2, 2010 Why don't you simply bindKey's and disable the AFK status when any of the bound keys are pressed? Link to comment
BinSlayer1 Posted August 2, 2010 Author Share Posted August 2, 2010 bind what key? I understand I should generalize, not make it for 1 key only Link to comment
50p Posted August 2, 2010 Share Posted August 2, 2010 Whatever keys you want... like "accelerate" or "break_reverse". addEventHandler( "onGamemodeMapStart", getRootElement( ), function( ) for i, plr in ipairs( getElementsByType( "player" ) ) do if not isKeyBound( plr, "down", "accelerate", unbindKeys ) then -- check if the keys have been bound already bindKeys( plr ); end end end ) function bindKeys( player ) setElementData( player, "overrideCollide.uniqueblah", 0, false ); bindKey( player, "down", "accelerate", unbindKeys ); bindKey( player, "down", "break_reverse", unbindKeys ); end function unbindKeys( player ) setElementData( player, "overrideCollide.uniqueblah", nil, false ); unbindKey( player, "down", "accelerate", unbindKeys ); unbindKey( player, "down", "break_reverse", unbindKeys ); end Link to comment
BinSlayer1 Posted August 3, 2010 Author Share Posted August 3, 2010 thanks, but it isn't working Link to comment
AcidbRain Posted August 3, 2010 Share Posted August 3, 2010 Try using the onMapStarting event by changing the first line to: addEventHandler( "onMapStarting", getRootElement( ), Link to comment
50p Posted August 3, 2010 Share Posted August 3, 2010 thanks, but it isn't working I never said it will work. Do you get any warning/error messages? I've heard from a few people that ghost mode doesn't work sometimes. Link to comment
BinSlayer1 Posted August 3, 2010 Author Share Posted August 3, 2010 I tried that using onMapStarting and it's half-working @ 50p: I don't get any errors.. I tried putting some debug text and it tells me gm is on and that it's also checking to see if the keys were already bound. GM does get enabled. But I don't get any text from the second part of the text which is unbindKeys and gm still stays enabled, it won't go off as I accelerate. Link to comment
AcidbRain Posted August 3, 2010 Share Posted August 3, 2010 gm still stays enabled, it won't go off as I accelerate. Are you using a controller in analog mode? Link to comment
BinSlayer1 Posted August 3, 2010 Author Share Posted August 3, 2010 no, i'm using the keyboard "w" to accelerate ahh i find it very frustrating that only half script works i wonder what'd be wrong with the unbindKeys function Link to comment
AcidbRain Posted August 3, 2010 Share Posted August 3, 2010 Look at the order of the key and keyState parameters (https://wiki.multitheftauto.com/wiki/BindKey), you have to switch them on the calls to bindKey and unbindKey. Link to comment
BinSlayer1 Posted August 3, 2010 Author Share Posted August 3, 2010 Thanks works like a charm now I would like to say thanks to everybody who posted here and helped me 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