Jump to content

[Help] triggerClientEvent


Perfect

Recommended Posts

Posted

Hello Everyone, Since few hours i am trying to run this simple script which uses 'trigerClientEvent'.

scripts:-

server:

  
myMessages = {  "Message 1", 
                "Message 2", 
                "Message 3" 
             } 
  
function startIt() 
    for i=1,3 do 
        triggerClientEvent(resourceRoot, "sendMessage", root, myMessages[i]) --I tried replacing 'root' with resourceRoot,source,v(looping getElementsByType('player') 
    end 
        outputChatBox("Resource started") 
end 
addEventHandler("onResourceStart", resourceRoot, startIt) 
  

client:

  
addEvent("sendMessage", true) 
  
function myMessage(message) 
    outputChatBox(message) 
end 
  
addEventHandler("sendMessage",resourceRoot,myMessage) 
  

The problem is when i start the resource nothing happened except 'Resource Started' message was showed and there were/are no errors/warnings in debugscript 3.

I tried removing 1st argument from 'triggerClientEvent' but that causes an error: Event is not added client side.

Hope you guys can help me in solving this. :)

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

Oh, thanks for info.

If i am right, then it can be fix with setTimer() but still there are any other ways to solve it ?

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

You mean, i need to do something like this ?

scripts:-

server:

  
addEvent("sendMessageAgain", true) 
myMessages = {  "Message 1", 
                "Message 2", 
                "Message 3" 
             } 
  
function sendMessageBack() 
    for i=1,3 do 
        triggerClientEvent(resourceRoot, "sendMessage", resourceRoot, myMessages[i]) 
    end 
        outputChatBox("Resource started") 
end 
addEventHandler("sendMessageAgain", resourceRoot, sendMessageBack) 
  

client:

  
addEvent("sendMessage", true) 
  
function myMessage(message) 
    outputChatBox(message) 
end 
  
addEventHandler("sendMessage",resourceRoot,myMessage) 
  
function startIt() 
    triggerServerEvent("sendMessageAgain", resourceRoot) 
end 
addEventHandler("onClientResourceStart", resourceRoot, startIt) 
  

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

"Resource started" will appear to all players when any player join and his/her resource started. And resourceRoot isn't player, you should use client.

CiTLh.png
Posted
addEvent("sendMessageAgain", true) 
myMessages = {  "Message 1", 
                "Message 2", 
                "Message 3" 
             } 
  
addEvent("sendMessageAgain", true) 
function sendMessageBack() 
    for i=1,3 do 
        triggerClientEvent(source, "sendMessage", source, myMessages[i]) 
    end 
        outputChatBox("Resource started") 
end 
addEventHandler("sendMessageAgain", resourceRoot, sendMessageBack) 

 

addEvent("sendMessage", true) 
function myMessage(message) 
    outputChatBox(message) 
end 
addEventHandler("sendMessage",resourceRoot,myMessage) 
  
function startIt() 
    triggerServerEvent("sendMessageAgain", localPlayer) 
end 
addEventHandler("onClientResourceStart", resourceRoot, startIt) 

Posted (edited)

Try to use my solution here , WhoAmI already gave it to you

Edited by Guest

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted (edited)

@TAPL, thanks,It solved the problem.

@WhoIAm,I already solved the problem with TAPL's solution but your solution may also work so thank you too.

@Walid,I tried that but it is a bit complicated for me but thanks for replying.

But now, i am in new problem.

I made a simple script which gets table data from server side and use it in client side but it is not working

It gives errors/warnings debuscript.

scripts:-

server:

  
--tables 
Pos =   {   {-2.73642, 24.87629, 114.14844}, 
            {12.32154, 51.76832, 14.14844}, 
            {-13.67212, -15.60738, 14.14844} 
        } 
myPed = {} 
     
for i=1,10 do 
    myPed[i] = createPed(0,Pos[i][1],Pos[i][2],Pos[i][3]) 
    setElementHealth(myPed[i], 50) 
end 
  
--trigger 
function triggerIt() 
    for i=1,10 do 
        triggerClientEvent("sendTableData", resourceRoot, myPed[i]) 
    end 
end 
function delayStart() 
    addEventHandler("onResourceStart", resourceRoot, triggerIt) 
end 
addEventHandler("onResourceStart", resourceRoot,  
function() setTimer(delayStart, 10000, 1) end ) 
  

client:

  
addEvent("sendTableData", true) 
  
function movePedForward(ped) 
    local players = getElementsByType('player') 
    local vehicles = getElementByType('vehicle') 
    for pi,p in pairs(players) do 
        setElementCollidableWith(ped, p, false) 
    end 
    for vi,v in pairs(vehicles) do 
        setElementCollidableWith(ped, v, false) 
    end 
end 
  
function pedRender(ped) 
            posx,posy = getElementPosition(ped) 
            setPedCameraRotation(ped, (posx+posy+180)) 
end 
  
--Event Handlers 
setTimer (function() addEventHandler("onClientRender", root, pedRender) end, 15000, 1 ) 
addEventHandler("sendTableData", root, pedRender) 
addEventHandler("sendTableData", root, movePedForward) 
  
  
  

debugscript:

WARNING: MTrigger\script_c.lua:15: Bad argument @ 'getElementPosition' [Expected elemant at argument 1, got nil]

ERROR:MTrigger\script_c.lua:16: attempt to perform arithmetic on global 'posx' (a boolean value)

Can someone please help me in solving this problem ?

Edited by Guest

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

Oh, sorry. that was a typo. still the same error/warning.

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

It looks like you didn't read my scripts as i want position of ped not a player, so maybe localplayer will not work.

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

Posted

Indeed, my bad.

You're triggering from serverside "SendTableData" event name, while you named your clientside event "sendTableData". That might be the reason of the ped element not being passed.

 

Posted

Thanks, I fixed setPedCameraRotation and sendTableData but still same errors occur in debugscript.

Debugscript:-

WARNING: MTrigger\script_c.lua:15: Bad argument @ 'getElementPosition' [Expected elemant at argument 1, got nil]

ERROR: MTrigger\script_c.lua:16: attempt to perform arithmetic on global 'posx' (a boolean value)

Multi Theft Auto Player since middle of 2011.

Everybody want to act Crazy/Smart/Intelligent/Different/Unique/Innocent and Thats why I am Perfect.

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