Jump to content

Triggering events and value returning


seal

Recommended Posts

I'll like to save the player's health to SQL database, but i dont know how to do it.

This is my script:(i'm new in lua scripting)

Serverside:

function callsavehp( thePlayer ) 
  triggerClientEvent ( thePlayer, "gethp", getRootElement()) 
  --outputChatBox(tostring(playerHealth)) 
end 
addCommandHandler("shp", callsavehp) 
  
function savehp(playerHealth) 
  outputChatBox(tostring(playerHealth)) 
end 
addEvent("savehp", true) 

Clintside:

function gethp() 
  local playerHealth = getElementHealth ( getLocalPlayer() ) 
  --return playerHealth 
  outputChatBox(tostring(playerHealth)) 
  outputChatBox("leves") 
  triggerClientEvent ( thePlayer, "savehp", getRootElement(),playerHealth) 
end 
addEvent("gethp", true) 

I've never used triggering on the right way, I tried to forbore to use it but it is relevant now( i think leastwise)

Link to comment

You're forgetting one thing, you are adding the events. But you aren't telling the system what these events are doing, you should add an addEventHandler to it. Like so;

function savehp ( playerHealth ) 
    outputChatBox ( tostring ( playerHealth ) ); 
end 
addEvent ( "savehp", true ); 
addEventHandler ( "savehp", getRootElement(), savehp ); 

Link to comment
You're forgetting one thing, you are adding the events. But you aren't telling the system what these events are doing, you should add an addEventHandler to it. Like so;
function savehp ( playerHealth ) 
    outputChatBox ( tostring ( playerHealth ) ); 
end 
addEvent ( "savehp", true ); 
addEventHandler ( "savehp", getRootElement(), savehp ); 

because of it added i clientside:

triggerClientEvent ( thePlayer, "savehp", getRootElement(),playerHealth)

and i tried your idea, but it didnt work

Link to comment

You have this in client side, but it serveer side function.

triggerClientEvent ( thePlayer, "savehp", getRootElement(),playerHealth) 

Use triggerServerEvent.

triggerServerEvent("savehp", localPlayer, playerHealth) 

Oh and i don't get the hell point of trigger from server side to client side and then trigger from client side to server side again!

This also weird you talking about SQL and you don't even know how to use trigger?

Link to comment

It's not an idea, it has to be done like that. xD Try this;

server;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        triggerClientEvent ( "getHealth", thePlayer ); 
    end 
); 
  
addEvent ( "saveHealth", true ); 
addEventHandler ( "saveHealth", root, 
    function ( thePlayer, health ) 
        outputChatBox ( "You have ".. health .." hp.", thePlayer ); 
    end 
); 

client;

addEvent ( "getHealth", true ); 
addEventHandler ( "getHealth", root, 
    function () 
        local health = getElementHealth ( getLocalPlayer() ); 
        triggerServerEvent ( "saveHealth", getLocalPlayer(), getLocalPlayer(), health ); 
    end 
); 

but why do you want to do a trigger to the clientside just to get the hp of the localPlayer, can be done way easier with just one single thing;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        local health = getElementHealth ( thePlayer ); 
        outputChatBox ( "You got ".. health .." hp.", thePlayer ); 
    end 
); 

Link to comment

It's not an idea, it has to be done like that. xD Try this;

server;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        triggerClientEvent ( "getHealth", thePlayer ); 
    end 
); 
  
addEvent ( "saveHealth", true ); 
addEventHandler ( "saveHealth", root, 
    function ( thePlayer, health ) 
        outputChatBox ( "You have ".. health .." hp.", thePlayer ); 
    end 
); 

client;

addEvent ( "getHealth", true ); 
addEventHandler ( "getHealth", root, 
    function () 
        local health = getElementHealth ( getLocalPlayer() ); 
        triggerServerEvent ( "saveHealth", getLocalPlayer(), getLocalPlayer(), health ); 
    end 
); 

but why do you want to do a trigger to the clientside just to get the hp of the localPlayer, can be done way easier with just one single thing;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        local health = getElementHealth ( thePlayer ); 
        outputChatBox ( "You got ".. health .." hp.", thePlayer ); 
    end 
); 

thanks a lot, is works,i dont know why want i to do it that way... :S

Link to comment

It's not an idea, it has to be done like that. xD Try this;

server;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        triggerClientEvent ( "getHealth", thePlayer ); 
    end 
); 
  
addEvent ( "saveHealth", true ); 
addEventHandler ( "saveHealth", root, 
    function ( thePlayer, health ) 
        outputChatBox ( "You have ".. health .." hp.", thePlayer ); 
    end 
); 

client;

addEvent ( "getHealth", true ); 
addEventHandler ( "getHealth", root, 
    function () 
        local health = getElementHealth ( getLocalPlayer() ); 
        triggerServerEvent ( "saveHealth", getLocalPlayer(), getLocalPlayer(), health ); 
    end 
); 

but why do you want to do a trigger to the clientside just to get the hp of the localPlayer, can be done way easier with just one single thing;

addCommandHandler ( "shp", 
    function ( thePlayer ) 
        local health = getElementHealth ( thePlayer ); 
        outputChatBox ( "You got ".. health .." hp.", thePlayer ); 
    end 
); 

thanks a lot, is works,i dont know why want i to do it that way... :S

what's the problem?

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