Jump to content

triggerClientEvent


fairyoggy

Recommended Posts

How to call a variable from the server side to the client? What's my mistake?

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", localPlayer, rain )

--server
function gap()
local kaka = "321"
triggerClientEvent(localPlayer, "rain", localPlayer, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

ERROR: attemp to concatenate local "kaka" (a nil value)

Link to comment

Hello slapztea,

  • line 2: on the clientside, the outputChatBox function does not take an element but a color number value as second argument.
  • line 11: the global variable "localPlayer" does not exist on the server-side; you either could use root to send to all players or select a specific player instead using getPlayerFromName/getElementsByType/etc.

I think that your script does have a working idea behind it; you just have to improve it a little. :) 

Edited by The_GTA
  • Like 1
Link to comment
9 hours ago, The_GTA said:

Здравствуйте, slapztea,

  • строка 2: на стороне клиента функция outputChatBox принимает не элемент, а значение номера цвета в качестве второго аргумента.
  • строка 11: глобальная переменная "localPlayer" не существует на стороне сервера; вы можете либо использовать root для отправки всем игрокам, либо выбрать конкретного игрока вместо этого, используя getPlayerFromName / getElementsByType / etc.

Я думаю, что у вашего сценария есть рабочая идея; вам просто нужно немного его улучшить.:) 

I do not yet understand how to solve this. Can you tell me how to use the element I need to transfer to the client?

Link to comment
28 minutes ago, slapztea said:

I do not yet understand how to solve this. Can you tell me how to use the element I need to transfer to the client?

I like helping you by example. I cannot understand the purpose behind your script so I can only give you general advice:

Most of the time just use root as baseElement for triggerClientEvent. If the event is limited to a resource then use resourceRoot. And if you want the event to be applied to a vehicle or object across resources then use that element.

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )

--server
function gap()
local kaka = "321"
triggerClientEvent("rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

You probably won't understand why I removed the first argument to triggerClientEvent, but only use localPlayer if you know which player element to send the event to. ??

Edited by The_GTA
  • Like 1
Link to comment
14 minutes ago, The_GTA said:

I like helping you by example. I cannot understand the purpose behind your script so I can only give you general advice:

Most of the time just use root as baseElement for triggerClientEvent. If the event is limited to a resource then use resourceRoot. And if you want the event to be applied to a vehicle or object across resources then use that element.


--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh",rain)
--server
function gap()
local kaka = "321"
triggerClientEvent("rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

You probably won't understand why I removed the first argument to triggerClientEvent, but only use localPlayer if you know which player element to send the event to. ??

I tried to use it like this

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh",rain)
--server
function gap()
local kaka = "321"
triggerClientEvent("rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

Why when do I use the command "gh" writes to chat "The client says: gh" but i need "The Client says: 321"

How to do it?

Link to comment
4 minutes ago, The_GTA said:

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh",function(cmdName, kaka) rain(kaka) end)

 

So I returned to the very first error

"ERROR: attemp to concatenate local "kaka" (a nil value)"

Link to comment

The reason is because you have not defined the variable "kaka" for the command handler "gh". If you type "/gh 321" into the chat, then it should work.

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh",
  function(cmdName, kaka)
    if not kaka then
      outputChatBox("missing kaka argument to gh command");
      return
    end
    rain(kaka)
  end
)

 

Edited by The_GTA
  • Like 1
Link to comment
2 minutes ago, The_GTA said:

The reason is because you have not defined the variable "kaka" for the command handler "gh". If you type "/gh 321" into the chat, then it should work.


--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh",
  function(cmdName, kaka)
    if not kaka then
      outputChatBox("missing kaka argument to gh command");
      return
    end
    rain(kaka)
  end
)

 

If do as you say it works BUT this is not what I need. 
For example, I type /gh 21421 and a chat message will appear "The client says: 21421" but i need so that the value is taken from a variable kaka from serverside. And it should turn out "The client says: 321" and nothing else, until the variable changes on the server side

Link to comment
13 minutes ago, slapztea said:

If do as you say it works BUT this is not what I need. 
For example, I type /gh 21421 and a chat message will appear "The client says: 21421" but i need so that the value is taken from a variable kaka from serverside. And it should turn out "The client says: 321" and nothing else, until the variable changes on the server side

OK. Let's take your requirements into account. Is a server-side commandHandler called "gh" fine for you?

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )

--server
function gap()
local kaka = "321"
triggerClientEvent("rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )
addCommandHandler("gh", function(p, cmdName) gap() end)

 

Edited by The_GTA
  • Like 1
Link to comment
4 minutes ago, The_GTA said:

OK. Let's take your requirements into account. Is a server-side commandHandler called "gh" fine for you?


--clientfunction rain(kaka)outputChatBox ( "The client says: " .. kaka, client )endaddEvent("rain", true)addEventHandler( "rain", resourceRoot, rain )--serverfunction gap()local kaka = "321"triggerClientEvent("rain", resourceRoot, kaka)endaddEvent( "gap", true )addEventHandler( "gap", resourceRoot, gap )addCommandHandler("gh", function(p, cmdName) gap() end)

 

Now it works as intended, but is it possible to implement the call of this command on the client?

Link to comment
10 minutes ago, slapztea said:

Now it works as intended, but is it possible to implement the call of this command on the client?

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh", function() triggerServerEvent("gap", resourceRoot) end)

--server
function gap()
local kaka = "321"
triggerClientEvent(client, "rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

EDIT: fixed something by adding "client" global variable. Sorry I have forgotten about the existance of it for a second. :P 
EDIT2: sorry another mistake fixed at addCommandHandler...

Edited by The_GTA
  • Like 1
Link to comment
2 minutes ago, The_GTA said:

--client
function rain(kaka)
outputChatBox ( "The client says: " .. kaka, client )
end
addEvent("rain", true)
addEventHandler( "rain", resourceRoot, rain )
addCommandHandler("gh", resourceRoot, function() triggerServerEvent("gap", resourceRoot) end)

--server
function gap()
local kaka = "321"
triggerClientEvent("rain", resourceRoot, kaka)
end
addEvent( "gap", true )
addEventHandler( "gap", resourceRoot, gap )

 

Now I don’t know what’s the matter. Nothing happens on command, writes nothing in debug

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