Jump to content

fetchRemote Problem


OpleX

Recommended Posts

hi , i just started use fetchRemote

and i tried the example from the wiki ( the image one ) , but it didn't work although the url is working ..

-- server
function ShowImage (player) 
    outputChatBox ( "event called, fetching image..." ) 
    fetchRemote ( "https://img.icons8.com/cotton/2x/baby.png",  
        function ( response, error ) 
            if error == 0 then 
                if player then 
                    triggerClientEvent ( player, "onClientGotImage", player, response ) 
                    outputChatBox ( "data sucessfully send to the player" ) 
                else 
                    outputChatBox ( "bad argument (player) ("..tostring(player)..")" ) 
                end 
            else     
                outputChatBox ( "error "..error.." when fetching image." ) 
            end 
        end 
    ) 
end 
addEventHandler("onResourceStart",root,ShowImage) 

--- client 
local myTexture 
  
addEvent( "onClientGotImage", true ) 
addEventHandler( "onClientGotImage", root, 
    function( pixels ) 
        outputChatBox ( "client: ok." ) 
         
        if myTexture and isElement ( myTexture ) then 
            destroyElement( myTexture ) 
        end 
        myTexture = dxCreateTexture( pixels ) 
    end 
) 
  
addEventHandler("onClientRender", root, 
    function() 
        if myTexture then 
            local w,h = dxGetMaterialSize( myTexture ) 
            dxDrawImage( 200, 100, w, h, myTexture ) 
        end 
    end )

it says data has been sent but nothing happens

thanks in advance

Link to comment
  • Discord Moderators

To see errors and warnings from your script, use /debugscript 3.

The problem here is you are parsing a resource element into triggerClientEvent where it expects a player.

function ShowImage (player) 

The player here is a resource, because you have bound it to onResourceStart.

If you want to send this image to everyone on the server, use root as "sendTo" parameter.

triggerClientEvent ( root, "onClientGotImage", root, response ) 

I've made a few changes to it, see the comments here:

-- server
function ShowImage() 
    outputChatBox ( "event called, fetching image..." ) 
    fetchRemote ( "https://img.icons8.com/cotton/2x/baby.png",  
        function ( response, error ) 
            if error == 0 then 
                triggerClientEvent ( root, "onClientGotImage", resourceRoot, response ) -- Use resourceRoot as the source of the event
                outputChatBox ( "data sucessfully send to the player" ) 
            else     
                outputChatBox ( "error "..error.." when fetching image." ) 
            end 
        end 
    ) 
end 
addEventHandler("onResourceStart", resourceRoot, ShowImage) -- Use resourceRoot otherwise it triggers when any resource starts

-- client
local myTexture 
  
addEvent( "onClientGotImage", true ) 
addEventHandler( "onClientGotImage", resourceRoot, -- resourceRoot as source 
    function( pixels ) 
        outputChatBox ( "client: ok." ) 
         
        if myTexture and isElement ( myTexture ) then 
            destroyElement( myTexture ) 
        end 
        myTexture = dxCreateTexture( pixels ) 
    end 
) 
  
addEventHandler("onClientRender", root, 
    function() 
        if myTexture then 
            local w,h = dxGetMaterialSize( myTexture ) 
            dxDrawImage( 200, 100, w, h, myTexture ) 
        end 
    end ) 

This code works for you on your local server, but you will run into issues doing this with other players, because they won't have loaded the clientside script before you send the image to them. Let me know if you need more explanation.

  • Like 1
Link to comment

i just used the example from the wiki to check if it will work , but apparently it didn't show anything on the Debug

..however , i need to create a avatar system that make the players code upload their avatars through URL and then it download for all the server so that it will be shown for everyone , i hope you get what i mean .. 

thanks for help .. i really appreciate that .

Link to comment
  • Moderators
12 hours ago, OpleX said:

i just used the example from the wiki to check if it will work , but apparently it didn't show anything on the Debug

..however , i need to create a avatar system that make the players code upload their avatars through URL and then it download for all the server so that it will be shown for everyone , i hope you get what i mean .. 

thanks for help .. i really appreciate that .

Another problem, which is most likely that the clients haven't download/load the scripts yet, while receiving the avatar.

 

The following code will trigger the onRequestImage event when the client has loaded his resource.

 

Server

function ShowImage () 
	local player = client
-- ...
  
  
addEvent("onRequestImage", true)
addEventHandler("onRequestImage", resourceRoot, ShowImage, false)

 

Client

addEventHandler("onClientResourceStart", resourceRoot,
function () 
	triggerServerEvent("onRequestImage", resourceRoot)
end, false)

 

 

 

Edited by IIYAMA
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...