Jump to content

[Help] Problems with triggerServerEvent


Recommended Posts

I am unable to update the elementdata of the generator marker when calling the serverEvent LigarGerador or DesligarGerador.

 

local screenW, screenH = guiGetScreenSize()

addEventHandler("onClientRender", root,
    function()
        if getElementData( localPlayer, "AbrirGerador" ) == true then
            local generatorID = getElementData( localPlayer, "generatorID")
            local currentFuelLevel = getElementData( localPlayer, "currentFuelLevel")
            local fuelLevel = getElementData( localPlayer, "fuelLevel")
            local maxFuelLevel = getElementData( localPlayer, "maxFuelLevel")
            local Estado = EstadoMotor( generatorID )

            dxDrawImage(screenW * 0.4103, screenH * 0.4401, screenW * 0.0235, screenH * 0.2734, ":[Looktovask]RevendaCarros/imagens/cube.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
            dxDrawText("Combustivel:", 412, 459, 548, 491, tocolor(255, 255, 255, 255), 1.00, "default", "right", "bottom", false, false, false, false, false)

            --progress gasolina
            local imageWidth = screenW * 0.0147
            local maxHeight = screenH * 0.2617
            local imageHeight = maxHeight * fuelLevel / maxFuelLevel

            dxDrawImage(screenW * 0.4147, screenH * 0.4453 + maxHeight - imageHeight, imageWidth, imageHeight, ":[Looktovask]RevendaCarros/imagens/cube.png", 0, 0, 0, tocolor(195, 181, 32, 206), false)

            if Estado == true then
                r,g,b = 74, 255, 80
                dxDrawText("Desligar", screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018, tocolor(255, 255, 255, 255), 1.00, "arial", "center", "center", false, false, false, false, false)
                dxDrawImage(screenW * 0.3132, screenH * 0.6471, screenW * 0.0941, screenH * 0.0664, ":[Looktovask]RevendaCarros/imagens/cube.png", 0, 0, 0, tocolor(r, g, b, 217), false)
            elseif Estado == false then
                r,g,b = 147, 74, 60
                dxDrawText("Ligar", screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018, tocolor(255, 255, 255, 255), 1.00, "arial", "center", "center", false, false, false, false, false)
                dxDrawImage(screenW * 0.3132, screenH * 0.6471, screenW * 0.0941, screenH * 0.0664, ":[Looktovask]RevendaCarros/imagens/cube.png", 0, 0, 0, tocolor(r, g, b, 217), false)
            end
        end
    end
)

function AbrirGeradorM( generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state, x, y, z )
    setElementData( localPlayer, "generatorID", generatorID )
    setElementData( localPlayer, "currentFuelLevel", currentFuelLevel )
    setElementData( localPlayer, "fuelLevel", fuelLevel )
    setElementData( localPlayer, "maxFuelLevel", maxFuelLevel )
    setElementData(localPlayer, "x", x)
    setElementData(localPlayer, "y", y)
    setElementData(localPlayer, "z", z)
    setElementData( localPlayer, "AbrirGerador", true )
end
addEvent( "AbrirGerador", true )
addEventHandler( "AbrirGerador", root, AbrirGeradorM )

function fecharGeradorM( generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state )
    setElementData( localPlayer, "AbrirGerador", false )
end
addEvent( "fecharGerador", true )
addEventHandler( "fecharGerador", root, fecharGeradorM )

-- clique
addEventHandler( "onClientClick", root, 
function (button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedWorld) 
    if button == "left" then
        if state == "down" then
            if getElementData( localPlayer, "AbrirGerador" ) == true then
                if isCursorOnElement(screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018) then
                    local state = getElementData( localPlayer, "state")
                    local x = getElementData(localPlayer, "x")
                    local y = getElementData(localPlayer, "y")
                    local z = getElementData(localPlayer, "z")
                    local id = getElementData( localPlayer, "generatorID" )
                    if EstadoMotor( id ) == true then
                        triggerServerEvent( "desligarGerador", resourceRoot, localPlayer, generatorID )
                    elseif EstadoMotor( id ) == false then
                        playSound3D( "start.mp3", x, y, z )
                        triggerServerEvent( "ligarGerador", resourceRoot, localPlayer, generatorID )
                    end
                end
            end
        end
    end
end)

---- Função Mouse ----
local x,y = guiGetScreenSize()
function isCursorOnElement(x, y, w, h)
    if (not isCursorShowing()) then
        return false
    end
    local mx, my = getCursorPosition()
    local fullx, fully = guiGetScreenSize()
    cursorx, cursory = mx*fullx, my*fully
    if cursorx > x and cursorx < x + w and cursory > y and cursory < y + h then
        return true
    else
        return false
    end
end

addEventHandler("onClientElementDataChange", root, 
function(dataName, oldValue)
    if (getElementData(source, "generatorID")) then
        if (dataName == "state") then
            if getElementData(source, "state") == true then
                local x,y,z = getElementPosition(source)
                local soundidle = playSound3D("idle.mp3", x, y, z, true)
            else
                destroyElement( soundidle )
            end
        end
    end
end)

function EstadoMotor( generatorID )
    local markers = getElementsByType("marker")
    for i, marker in ipairs(markers) do
        local id = getElementData(marker, "generatorID")
        if id == generatorID then
            estadomotor = getElementData( marker, "state" )
            return estadomotor
        end
    end
end

server:
 

-- Crie uma tabela contendo as informações de cada gerador
local geradores = {
    {id = 1, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {2397.336, -2472.24, 12.8}},
    {id = 2, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {150, 250, 10}},
    {id = 3, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {200, 300, 10}},
}

-- Crie os marcadores para cada gerador na tabela
for _, gerador in ipairs(geradores) do
    local marker = createMarker(gerador.position[1], gerador.position[2], gerador.position[3], "cylinder", 1, 255, 0, 0, 20)
    setElementData(marker, "generatorID", gerador.id)
    setElementData(marker, "fuelLevel", gerador.fuelLevel)
    setElementData(marker, "maxFuelLevel", gerador.maxFuelLevel)
    setElementData(marker, "state", gerador.state)
end

addEventHandler("onPlayerMarkerHit", root,
function (markerHit, matchingDimension)
    local generatorID = getElementData(markerHit, "generatorID")
    local fuelLevel = getElementData(markerHit, "fuelLevel")
    local maxFuelLevel = getElementData(markerHit, "maxFuelLevel")
    local state = getElementData(markerHit, "state")
    local x,y,z = getElementPosition( markerHit )
    triggerClientEvent(source, "AbrirGerador", root, generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state, x, y, z )
end)

addEventHandler("onPlayerMarkerLeave", root,
function (markerLeft, matchingDimension)
    local generatorID = getElementData(markerLeft, "generatorID")
    triggerClientEvent(source, "fecharGerador", root, generatorID)
end)

function desligarGeradorS( player, generatorID )
    local markers = getElementsByType("marker")
    for i, marker in ipairs(markers) do
        local id = getElementData(marker, "generatorID")
        if id == generatorID then
            setElementData(marker, "state", false)
        end
    end
end
addEvent( "desligarGerador", true )
addEventHandler( "desligarGerador", root, desligarGeradorS )

function ligarGeradorS( player, generatorID )
    local markers = getElementsByType("marker")
    for i, marker in ipairs(markers) do
        local id = getElementData(marker, "generatorID")
        if id == generatorID then
            setElementData(marker, "state", true)
        end
    end
end
addEvent( "ligarGerador", true )
addEventHandler( "ligarGerador", root, ligarGeradorS )

 

Link to comment

 Server side u have to do this!

local markers = {}
local geradores = {
    {id = 1, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {2397.336, -2472.24, 12.8}},
    {id = 2, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {150, 250, 10}},
    {id = 3, fuelLevel = 50, maxFuelLevel = 200, state = false, position = {200, 300, 10}},
}

-- Crie os marcadores para cada gerador na tabela
for key, gerador in ipairs(geradores) do
    local marker = createMarker(gerador.position[1], gerador.position[2], gerador.position[3], "cylinder", 1, 255, 0, 0, 20)
  	local markers[key] = marker
    setElementData(markers[key], "generatorID", gerador.id)
    setElementData(markers[key], "fuelLevel", gerador.fuelLevel)
    setElementData(markers[key], "maxFuelLevel", gerador.maxFuelLevel)
    setElementData(markers[key], "state", gerador.state)
end

addEventHandler("onPlayerMarkerHit", root,
function (markerHit, matchingDimension)
    for k,v in ipairs(markers) do 
    if markerHit == markers[k] then 
    local generatorID = getElementData(markers[k], "generatorID")
    local fuelLevel = getElementData(markers[k], "fuelLevel")
    local maxFuelLevel = getElementData(markers[k], "maxFuelLevel")
    local state = getElementData(markers[k], "state")
    local x,y,z = getElementPosition( markers[k] )
    triggerClientEvent(source, "AbrirGerador", root, generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state, x, y, z )
        end
    end
end)
    
addEventHandler("onPlayerMarkerLeave", root,
function (markerLeft, matchingDimension)
     for k,v in ipairs(markers) do 
     if markerLeft == markers[k] then 
    local generatorID = getElementData(markers[k], "generatorID")
    triggerClientEvent(source, "fecharGerador", root, generatorID)
      	end
    end
end)

function desligarGeradorS( player, generatorID )
    for i, marker in ipairs(markers) do
        local id = getElementData(markers[i], "generatorID")
        if id == generatorID then
            setElementData(markers[i], "state", false)
        end
    end
end
addEvent( "desligarGerador", true )
addEventHandler( "desligarGerador", root, desligarGeradorS )

function ligarGeradorS( player, generatorID )
    for i, marker in ipairs(markers) do
        local id = getElementData(markers[i], "generatorID")
        if id == generatorID then
            setElementData(markers[i], "state", true)
        end
    end
end
addEvent( "ligarGerador", true )
addEventHandler( "ligarGerador", root, ligarGeradorS )

I just edited your code so you wont have a bug with the markers because u made the marker hit event to work for any marker u hit not only those 3 markers!

Also client side u can do this here!

local data = {} -- or u name it the way u want 
function AbrirGeradorM( generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state, x, y, z )
 	data["id"] = generatorID
  	data["currentFuelLevel"] = currentFuelLevel
  	data["fuelLevel"] = fuelLevel
  	data["maxFuelLevel"] = maxFuelLevel 
  	data["state"] = state 
  	data["x"] = x 
  	data["y"] = y
  	data["z"] = z
    setElementData( localPlayer, "AbrirGerador", true )
end
addEvent( "AbrirGerador", true )
addEventHandler( "AbrirGerador", root, AbrirGeradorM )

I think this should fix the problem.

And on client side u can use data from the table all over the script u dont need to setElementData to localPlayer

 

Link to comment

Got it fixed! Somehow i replaced your image draw with a button for test! Also dont forget to put your picture back on client side part.

Server side

function desligarGeradorS( player, generatorID )
        local id = getElementData(markers[generatorID], "generatorID")
        if id == generatorID then
            setElementData(markers[generatorID], "state", false)
    end
end
addEvent( "desligarGerador", true )
addEventHandler( "desligarGerador", root, desligarGeradorS )

function ligarGeradorS( player, generatorID )
        local id = getElementData(markers[generatorID], "generatorID")
        if id == generatorID then
            setElementData(markers[generatorID], "state", true)
    end
end

addEvent( "ligarGerador", true )
addEventHandler( "ligarGerador", root, ligarGeradorS )

Client Side

local data = {} -- or u name it the way u want 
local screenW, screenH = guiGetScreenSize()

function AbrirGeradorM( generatorID, currentFuelLevel, fuelLevel, maxFuelLevel, state, x, y, z )
 	data["id"] = generatorID
  	data["currentFuelLevel"] = currentFuelLevel
  	data["fuelLevel"] = fuelLevel
  	data["maxFuelLevel"] = maxFuelLevel 
  	data["state"] = state 
  	data["x"] = x 
  	data["y"] = y
  	data["z"] = z
    setElementData( localPlayer, "AbrirGerador", true )
end
addEvent( "AbrirGerador", true )
addEventHandler( "AbrirGerador", root, AbrirGeradorM )

addEventHandler("onClientRender", root,
    function()
        if getElementData( localPlayer, "AbrirGerador" ) == true then
         --   if a == 0 then
           --     a = 1 
            local generatorID = data["id"]
            local currentFuelLevel = data["currentFuelLevel"]
            local fuelLevel = data["fuelLevel"] 
            local maxFuelLevel = data["maxFuelLevel"]

            if fuelLevel then
             --   showCursor(true,true)
           -- test = guiCreateButton(310, 359, 98, 50, "test", false)
           -- addEventHandler("onClientGUIClick",test,aaa,false)     -- replace back with your picture i took here one random for testing
            dxDrawImage(screenW * 0.4103, screenH * 0.4401, screenW * 0.0235, screenH * 0.2734, "cube.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
            dxDrawText("Combustivel:", 412, 459, 548, 491, tocolor(255, 255, 255, 255), 1.00, "default", "right", "bottom", false, false, false, false, false)

            --progress gasolina
            local imageWidth = screenW * 0.0147
            local maxHeight = screenH * 0.2617
            local imageHeight = maxHeight * fuelLevel / maxFuelLevel

            dxDrawImage(screenW * 0.4147, screenH * 0.4453 + maxHeight - imageHeight, imageWidth, imageHeight, "cube.png", 0, 0, 0, tocolor(195, 181, 32, 206), false)

            if data["state"] == true then
                r,g,b = 74, 255, 80
                dxDrawText("Desligar", screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018, tocolor(255, 255, 255, 255), 1.00, "arial", "center", "center", false, false, false, false, false)
                dxDrawImage(screenW * 0.3132, screenH * 0.6471, screenW * 0.0941, screenH * 0.0664, "cube.png", 0, 0, 0, tocolor(r, g, b, 217), false)
            elseif data["state"] == false then
                r,g,b = 147, 74, 60
                dxDrawText("Ligar", screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018, tocolor(255, 255, 255, 255), 1.00, "arial", "center", "center", false, false, false, false, false)
                dxDrawImage(screenW * 0.3132, screenH * 0.6471, screenW * 0.0941, screenH * 0.0664, "cube.png", 0, 0, 0, tocolor(r, g, b, 217), false)
                end
            
        end
    end
end)

addEventHandler( "onClientClick", root, 
function (button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedWorld) 
    if button == "left" then
        if state == "down" then
            if getElementData( localPlayer, "AbrirGerador" ) == true then
                if isCursorOnElement(screenW * 0.3206, screenH * 0.6602, screenW * 0.3963, screenH * 0.7018) then
                    local state = data["state"]
                    local x = data["x"] 
                    local y = data["y"]  
                    local z = data["z"]  
                    local id = data["id"]  
                    if data["id"] and data["state"] == true then
                        triggerServerEvent( "desligarGerador", resourceRoot, localPlayer, data["id"] )
                    elseif data["id"] and data["state"] == false then
                        playSound3D( "start.mp3", x, y, z )
                        triggerServerEvent( "ligarGerador", resourceRoot, localPlayer, data["id"] )
                    end
                end
            end
        end
    end
end)

 

Edited by FlorinSzasz
  • Thanks 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...