Jump to content

Having trouble making a script :)


BinSlayer1

Recommended Posts

Hi!

I'm a beginner ( fine, call me noob :P ) 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

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
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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...