Jump to content

onColShapeHit


Recommended Posts

hello... there is a problem i have in my code when the player enters the ColShape nothing happens.. no errors no nothing

code:

function giveBombToRandomPlayer1()
local randomPlayer = getRandomPlayer ()
  local tT = getTeamFromName ("Terrorists")
    local playerTeam = getPlayerTeam ( randomPlayer ) 
	  if ( playerTeam ) == tT then
	    outputChatBox("You have the bomb!", randomPlayer, 255, 0, 0)
		setElementData(randomPlayer,"hasBomb1",true)
	end
end

bombA = createRadarArea ( 3118.7380371094, -1407.4719238281, 20, 20, 255, 0, 0, 150 )
bombAEnter = createColRectangle( 3118.7380371094, -1407.4719238281, 20, 20)

function whenEnterbomb1(hitPlayer)
 if getElementType(hitPlayer)=="player" then
      if getElementData(hitPlayer,"hasBomb1") == true then
	    outputChatBox("Press 'g' to plant the bomb!",hitPlayer, 255, 0, 0)
		setElementData(hitPlayer,"canPlant1",true)
		end
    end
end
addEventHandler( "onColShapeHit", bombAEnter, whenEnterbomb1)
 
function whenLeaveBomb1(hitPlayer)
 if getElementType(hitPlayer)=="player"then
       if getElementData(hitPlayer,"hasBomb1") == true then
		setElementData(hitPlayer,"canPlant1",false)
		setElementData(hitPlayer,"hasBomb1",false)
        end
    end
end
addEventHandler( "onColShapeLeave", bombAEnter,whenLeaveBomb1)

 

Link to comment

yes.. it output you have the bomb

but the other output doesn't show when i enter the col

nvm i fixed it, but just a question.. will it always give to a random player from tT team?

Edited by Avagard
Link to comment

No, you will need a loop to get a new random player until it finds one that's in that team. It would be easier to get the list of players in that team using getPlayersInTeam and use math.random to get a random player from the returned table.

  • Like 1
Link to comment
On 5/8/2017 at 17:12, pa3ck said:

No, you will need a loop to get a new random player until it finds one that's in that team. It would be easier to get the list of players in that team using getPlayersInTeam and use math.random to get a random player from the returned table.

will this work?

function giveBombToRandomPlayer1()
	   local players = getPlayersInTeam ( T )
          for playerKey, playerValue in ipairs ( players ) do
            local randomPlayer = getRandomPlayer ()
		         setTimer(function()
		           outputChatBox("You have the bomb!", randomPlayer, 255, 0, 0)
		            outputChatBox("Remember when you enter site.. switch to the C4 to plant!", randomPlayer, 255, 0, 0)
		               giveWeapon ( randomPlayer, 14,1,true)
		         end,6000,1)	
	    end
end

 

Link to comment

No, look at this code:

function giveBombToRandomPlayer()
	local teamElement = getTeamFromName ("Terrorists")
	local teamMembers
  	if isElement(teamElement) then
    	teamMembers = getPlayersInTeam(teamElement)
    else
    	outputChatBox("Team is not available")
    	return
   	end
  
  	if(teamMembers and #teamMembers > 0 then)
    	local randomIndex = math.random(1, #teamMembers) -- get a random index: between 1 and the length of the table
    	local randomPlayer = teamMembers[randomIndex] -- we already have a random index, now get it from the table
    	setTimer(function()
        	if not isElement(randomPlayer) then -- there's a 6 seconds timer here, there's a chance the terrorist we picked got disconnected, if so, find a new one
          		outputChatBox("Player is no longer available, finding new player...")
          		giveBombToRandomPlayer()
        		return  	
        	end
        	outputChatBox("You have the bomb!", randomPlayer, 255, 0, 0)
			outputChatBox("Remember when you enter site.. switch to the C4 to plant!", randomPlayer, 255, 0, 0)
			giveWeapon ( randomPlayer, 14,1,true)
		end,6000,1)	
    else
    	outputChatBox("Couldn't find any Terrorists")
	end
end

PS. this code editor is so annoying, I miss the old one, this one always fks up the indentation... not to mention the copy code function, it copies the line numbers and empty lines after each statement...

 

It is not tested, if you get any errors and you can't solve it yourself, feel free to come back with the errors you're getting.

Edited by pa3ck
  • Like 1
Link to comment
function giveBombToRandomPlayer()
	local teamElement = getTeamFromName ("Terrorists")
	local teamMembers = {}
  	if isElement(teamElement) then
    	for k, p in ipairs(getPlayersInTeam(teamElement)) do
      		if getElementDimension(p) == 0 then
        		table.insert(teamMembers, p )
        	end
      	end
    else
    	outputChatBox("Team is not available")
    	return
   	end
  
  	if(teamMembers and #teamMembers > 0 then)
    	local randomIndex = math.random(1, #teamMembers) -- get a random index: between 1 and the length of the table
    	local randomPlayer = teamMembers[randomIndex] -- we already have a random index, now get it from the table
    	setTimer(function()
        	if not isElement(randomPlayer) then -- there's a 6 seconds timer here, there's a chance the terrorist we picked got disconnected, if so, find a new one
          		outputChatBox("Player is no longer available, finding new player...")
          		giveBombToRandomPlayer()
        		return  	
        	end
        	outputChatBox("You have the bomb!", randomPlayer, 255, 0, 0)
			outputChatBox("Remember when you enter site.. switch to the C4 to plant!", randomPlayer, 255, 0, 0)
			giveWeapon ( randomPlayer, 14,1,true)
		end,6000,1)	
    else
    	outputChatBox("Couldn't find any Terrorists")
	end
end

That should work

  • Like 1
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...