Jump to content

[HELP] How to make your own event handlers.


Recommended Posts

Hi,

I have seen some scripts and they have there own custom made event handlers.

I just want to know, where do I start so I can make my own?

Sorry, I don't have a script to show you because I have no clue on where to start making one.

I know how to make exports, just not event handlers.

Thanks.

Link to comment

I guess you could use something like this:

-- CLIENT -- 
button = createButton ( 0, 0, 50, 25, "Register", false ) 
addEventHandler ( "onClientGUIClick", root, -- I use "Root" because, if you use the "button" it will make a weird bug 
    function ( ) 
        if ( source == button ) then 
            triggerServerEvent ( "onPlayerRegister", localPlayer ) 
        end 
    end  
)  
  
-- SERVER -- 
addEvent ( "onPlayerRegister", true ) 
addEventHandler ( "onPlayerRegister", root, function ( ) 
    outputChatBox ( getPlayerName ( source ).." HAS REGISTERED!", root, 0, 255, 0 ) 
end ) 

Link to comment

nono, I mean so the event can be universal for all scripts.

So, if I made a core script and it contained something like onPlayerResigned.

Then I made a job so when he resigns, he gets some cash, so I would use the event handler as onPlayerResigned.

Get what I mean? Like an export so I can use it in other scripts.

Link to comment
nono, I mean so the event can be universal for all scripts.

So, if I made a core script and it contained something like onPlayerResigned.

Then I made a job so when he resigns, he gets some cash, so I would use the event handler as onPlayerResigned.

Get what I mean? Like an export so I can use it in other scripts.

I guess you could use something like this:
-- CLIENT -- 
button = createButton ( 0, 0, 50, 25, "Register", false ) 
addEventHandler ( "onClientGUIClick", root, -- I use "Root" because, if you use the "button" it will make a weird bug 
    function ( ) 
        if ( source == button ) then 
            triggerServerEvent ( "onPlayerRegister", localPlayer ) 
        end 
    end  
)  
  
-- SERVER -- 
addEvent ( "onPlayerRegister", true ) 
addEventHandler ( "onPlayerRegister", root, function ( ) 
    outputChatBox ( getPlayerName ( source ).." HAS REGISTERED!", root, 0, 255, 0 ) 
end ) 

^^ That is what that code will do.

Link to comment

Maybe this could help you :

Using Table

Try this (but debug first i made it in 2 Minutes without checking for bugs or else)

  
-- Pleas add [TiLex Produkt] as commentary to your script while using this  
  
addEvent("onPlayerHealthChange", true) 
local playerOldHealthTable = {} 
  
function healthChangedTriggerFunction() 
    setTimer(function() 
        local playerTable = getElementsByType("player") 
        for index, player in ipairs(playerTable) do 
        local oldLive = playerOldHealthTable[player] 
            if oldLive and (oldLive > 0) then 
                local curLive = getElementHealth(player) 
                if not (curLive == oldLive) then 
                 
                local changedlive = 0 
                if (curLive > oldLive) then 
                    changedlive = (curLive - oldLive) 
                else 
                    changedlive = (oldLive - curLive) 
                end 
                    triggerEvent ( "onPlayerHealthChange", player, player, curLive, oldLive, changedlive) -- trigger Event 
                    playerOldHealthTable[player] = curLive 
                end 
            else 
                playerOldHealthTable[player] = (getElementHealth(player)) 
            end 
        end 
    end, 500, 0) 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), healthChangedTriggerFunction) 
  
  
  
-- EXAMPEL FUNKTION FOR EVENT 
function healthChanged(player, newHealth, oldHealth, changedLive) 
    -- Live changed. Here some stuff 
outputChatBox("Your Live had changed from " ..tostring(oldHealth).. " to " ..tostring(newHealth)), player, 255, 0, 0) 
end 
addEventHandler("onPlayerHealthChange", getRootElement(), healthChanged) 
  

EventName: onPlayerHealthChange

Parameters: element Player, int newhealth, int oldHealth, int howManyHealthChanged

[sERVERSIDE]

Link to comment

A script more short.

function onAdminWasted(_, killer) 
    if killer and getElementType(killer) == "player" then 
        local playerAcc = getPlayerAccount(source) 
        local accName = getAccountName(playerAcc) 
    if isObjectInACLGroup ("user."..accName, aclGetGroup ( "Admin" ) ) then 
        triggerEvent("onAdminWasted", source, killer) 
    end 
  end 
end 
addEventHandler("onPlayerWasted", root, onAdminWasted) 
  
--Another Resource 
addEvent("onAdminWasted", true) 
  
function testMyNewEvent(thePlayer)--Parameters(1) : thePlayer : player who killed the admin. Source: the source of this event is the admin who was killed. 
    kickPlayer(thePlayer, "You dont allowed to kill an admin.") 
    outputChatBox("The player '"..getPlayerName(thePlayer).."' has been kicked because he/she killed you.", source, 0, 255, 0, true) 
end 
addEventHandler("onAdminWasted", root, testMyNewEvent) 

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