Jump to content

triggerClientEvent


FutScripter

Recommended Posts

(Sorry if the text contains errors, I'm Russian).

 

Hello! I decided to make a telephone. As we know, in order to create a picture on the screen, we need a client. But, since the main part is on the server, you need to call the client function on the server.

It seems like I did everything right (you can see the screenshots below), but it does not work: the picture does not appear. No errors are displayed in the console. Can you please help with this error?

 

Server

Client

Link to comment
  • Moderators

Hi.
Okay, so there are 3 problems with your code, what I can see now.

1) You have to use every dxDraw... functions inside a render event (onClientRender), because when you call these functions, the image visible only for 1 frame.

2) On client-side 3rd parameter of addEventHandler is missing, the attached function.

3) On server-side your triggerClientEvent is bad, because now you send this trigger to every(!) clients, you have the define "sendTo" parameter, it's root (everyone) by default.

Should look something like this:
 

-- CLIENT

function phoneRender()
    dxDrawImage(540, 290, 281, 512, "images/phone.png")
end

function openPhone()
    addEventHandler("onClientRender", root, phoneRender)
end
addEvent("openPhone", true)
addEventHandler("openPhone", root, openPhone)

-- how to stop render:
function closePhone()
    removeEventHandler("onClientRender", root, phoneRender)
end

 

-- SERVER
-- only the trigger

-- send this trigger only to "player", who entered the command
triggerClientEvent(player, "openPhone", root)

 

Here you can read about events: https://forum.multitheftauto.com/topic/114541-tut-events/

 

Edited by Patrick
  • Thanks 1
Link to comment
20 hours ago, Patrick said:

Hi.
Okay, so there are 3 problems with your code, what I can see now.

1) You have to use every dxDraw... functions inside a render event (onClientRender), because when you call these functions, the image visible only for 1 frame.

2) On client-side 3rd parameter of addEventHandler is missing, the attached function.

3) On server-side your triggerClientEvent is bad, because now you send this trigger to every(!) clients, you have the define "sendTo" parameter, it's root (everyone) by default.

Should look something like this:
 


-- CLIENT

function phoneRender()
    dxDrawImage(540, 290, 281, 512, "images/phone.png")
end

function openPhone()
    addEventHandler("onClientRender", root, phoneRender)
end
addEvent("openPhone", true)
addEventHandler("openPhone", root, openPhone)

-- how to stop render:
function closePhone()
    removeEventHandler("onClientRender", root, phoneRender)
end

 


-- SERVER
-- only the trigger

-- send this trigger only to "player", who entered the command
triggerClientEvent(player, "openPhone", root)

 

Here you can read about events: https://forum.multitheftauto.com/topic/114541-tut-events/

 

Oh, thank you very much! You helped me a lot!

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