Jump to content

[Help] triggerClientEvent


Perfect

Recommended Posts

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

Link to comment

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) 
  

Link to comment
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) 

Link to comment

@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
Link to comment

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)

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