Jump to content

1command2functions


GabWas

Recommended Posts

Hi there!

I'm new at scripting and I'm making my first script (bomb). It's maybe a little complicated for a beginner, but how hard could it be? :lol:

So, I have a question. Is there any way to put 2 functions into 1 command using CommandHandler? Because when the bomb is planted, you can plant another one, but then you can't detonate previous one. I need some kind of blockade, so you can plant only one bomb at the time.

I would be very thankful for your help, guys :)

Link to comment

Use variables!

This may help you:

local isBombPlanted = false; 
  
function plant(player) 
    if (isBombPlanted == false) then 
        -- Make the bomb plant here! 
        outputChatBox("You planted the bomb!", player, 255, 255, 255) 
        isBombPlanted = true 
    else 
        outputChatBox("The bomb is already planted!", player, 255, 255, 255) 
    end 
end 
addCommandHandler("bomb", plant) 
  
function bombExplode() 
    -- Make the bomb explode! 
    -- When the bomb explode set isBombPlanted to false so you can plant ti again! 
    isBombPlanted = false 
end 
addCommandHandler("explode", bombExplode) 

Link to comment
This is not very effective because other players will not be able to plant a bomb if another player already did it.

He can make it client-side, or maybe he need the bomb to be planted by only one player like in CS.

Yes, something like that :lol:

Do you maybe have idea about defusing? I thought about defusing, when a player is in invisible marker (created with model of bomb), but I just don't know how to do it. I tried onMarkerHit, but I've got an error with addEventHandler :oops:

Link to comment

Just use tables and do everything server side since you definitely don't want to bomb yourself lol. You are placing a bomb so it can explode other players, right? So do it sever side.

  
local bombs = {} 
  
addCommandHandler ("bomb", 
function ( p, cmd ) 
       if not bombs[getPlayerSerial (p) ]  then       
   local x, y, z = getElementPosition(p) 
   local colShape = createColSphere ( x, y, z, 1 ) 
                      bombs[getPlayerSerial (p) ] = colShape -- save the xyz 
  
              else 
                      -- bomb is already planted for current player 
       end 
end ) 
  
  
addCommandHandler ("explode", 
function ( p, cmd ) 
      if bombs[getPlayerSerial (p) ]) then 
local x, y, z = getElementPosition ( bombs[getPlayerSerial (p) ] ) 
             destroyElement (bombs[getPlayerSerial (p) ]) 
              bombs[getPlayerSerial (p) ] = nil 
           createExplosion ( x, y, z,8     ) 
end ) 
  
  
addEventHandler ( "onColShapeHit", resourceRoot, -- resourceRoot will only allow this event handler to get triggered if colshape is created by this resource where this script is currently running 
function ( elem ) 
       if getElementType(elem) == "player" then 
-- source of this event is the colshape 
              local x, y, z  = getElementPosition ( source ) 
                createExplosion ( x, y, z, 8    ) 
        destroyElement ( source ) 
    end 
end 
) 
  

serial is always good to use because it won't change even if player reconnects but if you want to remove bomb when player disconnects from server then set this same thing to nil, for example,

addEventHandler("onPlayerQuit", root, 
function () 
  if bombs[getPlayerSerial (source) ]) then 
             destroyElement (bombs[getPlayerSerial (source) ]) 
              bombs[getPlayerSerial (source) ] = nil 
   end 
 --this (nil) will remove the entry completely from the table. Don't use false use nil if you want to remove it and save memory just like I did 
end) 

I wrote this script from mobile. If you find any errors then fix it on your own.

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