Jump to content

Trigger server/client event doesn't work


Ziemo

Recommended Posts

Hi, I have a little problem with my script. I wanted to use server variable in client-side script, so I used trigger server and client event, but unfortunately, it didn't work. Do you know why?

PS: This script has to display variable characterName in grid row.

SERVER SIDE SCRIPT:

function getCharacterName(characterName)
        local characterName = 'James'
        triggerClientEvent ( thePlayer, 'getCharName', thePlayer, characterName )
end
addEvent('getCharacterName', true)
addEventHandler('getCharacterName', root, getCharacterName)

CLIENT-SIDE SCRIPT:

function openCharacterMenu(characterName)
    menu = guiCreateWindow(0.5, 0.5, 0.2, 0.25, 'Characters', true)
    charactersList = guiCreateGridList(0.5, 0.5, 0.4, 0.6, true, menu)
    charactersColumn = guiGridListAddColumn(charactersList, 'Characters', 0.6)
    charactersRow = guiGridListAddRow(charactersList, 'No characters found')
  
  guiGridListSetItemText(charactersList, charactersColumn, charactersRow, characterName, false, false)
end
addEvent('getCharName', true)
addEventHandler('login.success', localPlayer, openCharacterMenu)

I see one error in client debug: Bad argument @ 'gu'iGridListSetItemText [expected string at argument 4 got, nil]

Event 'login.success' opens up characters gui

Link to comment
  • Moderators
6 minutes ago, Ziemo said:

But it won't open up the whole gui. I want to open this gui after successful client login. 

 

You cannot change two tasks in to one task, that is not logic, even for humans.

 

Task 1.

Send over the name. (Save in to a variable)

 

 

Task 2.

Open the GUI and use the variable.

Link to comment
21 minutes ago, IIYAMA said:

 

You cannot change two tasks in to one task, that is not logic, even for humans.

 

Task 1.

Send over the name. (Save in to a variable)

 

 

Task 2.

Open the GUI and use the variable.

So how can i make a function that calls server-side variable?

Link to comment
1 hour ago, IIYAMA said:

clientside variable. You must have already send it before you login.

so client variable should look like this?

characterName = triggerServerEvent('getCharacterName', localPlayer, characterName)

 

Link to comment
  • Moderators

triggerServerEvent cannot send that data back like that. We are talking about two different systems that have a connection delay in between. (also known as PING)

 

If you want that name clientside and clientside is the one that is requesting it.

 

A clientside (request) > B serverside (getting the name) > clientside (receive)

A triggerServerEvent > B triggerClientEvent

Link to comment
  • Scripting Moderators
44 minutes ago, Ziemo said:

so this variable in client script should look like this?


characterName = triggerServerEvent('getCharacterName', localPlayer, characterName)

 

triggerServerEvent:

Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter.

I'll show you an example, to make things easier for you. It wasn't tested, but should work.

-- clientside

local first_variable = "Empty"
local second_variable = "Nothing here"

triggerServerEvent("onPlayerUseItem", getLocalPlayer(), first_variable, second_variable)

-- serverside

function onPlayerUseItem(first_arg, second_arg)
	local player = source -- event source
	outputChatBox(first_arg..", "..second_arg, player)
end
addEvent("onPlayerUseItem", true)
addEventHandler("onPlayerUseItem", getRootElement(), onPlayerUseItem)

This example shows how to get data from trigger, and if you want you can save it to variable.

Edited by majqq
  • Thanks 1
Link to comment
2 hours ago, majqq said:

triggerServerEvent:


Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter.

I'll show you an example, to make things easier for you. It wasn't tested, but should work.


-- clientside

local first_variable = "Empty"
local second_variable = "Nothing here"

triggerServerEvent("onPlayerUseItem", getLocalPlayer(), first_variable, second_variable)

-- serverside

function onPlayerUseItem(first_arg, second_arg)
	local player = source -- event source
	outputChatBox(first_arg..", "..second_arg, player)
end
addEvent("onPlayerUseItem", true)
addEventHandler("onPlayerUseItem", getRootElement(), onPlayerUseItem)

This example shows how to get data from trigger, and if you want you can save it to variable.

I think it will work, but I want to make the opposite thing, pass a variable from server to client.

Link to comment

I changed my code to this:

SERVER:

local characterName = 'John Doe'

triggerClientEvent('showCharacters', root, characterName)

CLIENT:

function showCharacters(characterName)
	outputChatBox(characterName, localPlayer)
end
addEvent('showCharacters', true)
addEventHandler('onResourceStart', localPlayer, showCharacters)

But it isn't working. I have an error: Server triggered clientside event showCharacters, but event is not added clientside.

Link to comment
  • Moderators
34 minutes ago, Ziemo said:

I changed my code to this:

SERVER:


local characterName = 'John Doe'

triggerClientEvent('showCharacters', root, characterName)

CLIENT:


function showCharacters(characterName)
	outputChatBox(characterName, localPlayer)
end
addEvent('showCharacters', true)
addEventHandler('onResourceStart', localPlayer, showCharacters)

But it isn't working. I have an error: Server triggered clientside event showCharacters, but event is not added clientside.

  1. Because the client hasn't loaded yet.
  2. Because the addEventHandler hasn't the right event. Should be "showCharacters"
  3. The base Element is incorrect. You sending the parent = root from serverside element to a handler with the child = localPlayer at clientside.
    root (container) > localPlayer (child) < NOT WORKING
    localPlayer (child) > root (container) < WORKING
     
  4. (You are sending a triggerClientEvent, without target, so sending it to every player in game.)

 

In case of sending it directly after loading the resource: (best practice)

 

CLIENT

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

 

SERVER

local characterName = 'John Doe'

addEvent("onThisClientResourceStart", true)
addEventHandler("onThisClientResourceStart", resourceRoot, 
function () 
    -- client is the target (the player that executed the event: onThisClientResourceStart)
	triggerClientEvent(client, 'showCharacters', resourceRoot, characterName) 
end, false)

 

CLIENT

function showCharacters(characterName)
	outputChatBox(characterName) -- localPlayer < this is running on the client/player his pc. No need to define the receiver, as only this player can receive the message
end
addEvent('showCharacters', true)
addEventHandler('showCharacters', resourceRoot, showCharacters, false)

 

 

 

 

 

 

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