Jump to content

[HELP] triggerClientEvent dont work


Recommended Posts

Hi, i wrote a script, then i want to start the script on resourceStart but my triggerClientEvent doesnt work. Why?
 

Client Side:

function createMinigunWeapon()
    -- Create the weapon 1 meter above the player
    local x, y, z = getElementPosition(localPlayer)
    local weapon = createWeapon("minigun", x, y, z + 1)
    -- Give it some ammo and fire it
    setWeaponClipAmmo(weapon, 99999)
    setWeaponState(weapon, "firing")

    setWeaponProperty(weapon, "fire_rotation", 0, 0, 0)
end
addEventHandler("createMinigun", localPlayer, createMinigunWeapon)
addEvent("createMinigun", true)

Server Side:

function createMinigunWeapon()
    triggerClientEvent(localPlayer, "createMinigun")
end
addEventHandler("onResourceStart", getRootElement(), createMinigunWeapon)

DebugScript Says:

WARNING: fs_raindSit\server.lua:36: Bad argument @ 'triggerClientEvent' [Expected string at argument 1, got nil]

Link to comment

predefined variable localPlayer doesn't exist serverside. You can direclty set the name as first argument, It'll send to root element by default, and so every players children.

bool triggerClientEvent ( [ table/element sendTo = getRootElement(), ] string name, element sourceElement [, arguments... ] )

Set the sourceElement (3rd parameter), I suggest resourceRoot (the present resource).

triggerClientEvent("eventName", resourceRoot)

You can aim specific players by passing a table as first parameter (or loop).

By the way be careful with your addEventHandler("onResourceStart", ...). Set getRootElement() (same as root predefined variable) will have for result to trigger the function everytime a resource is starting. You should rather use resourceRoot (for the actual resource).

addEventHandler("onResourceStart", resourceRoot, createMinigunWeapon)

 

Edited by Mkl
  • Thanks 1
Link to comment
22 minutes ago, Mkl said:

predefined variable localPlayer doesn't exist serverside. You can direclty set the name as first argument, It'll send to root element by default, and so every players children.

bool triggerClientEvent ( [ table/element sendTo = getRootElement(), ] string name, element sourceElement [, arguments... ] )

Set the sourceElement (3rd parameter), I suggest resourceRoot (the present resource).

triggerClientEvent("eventName", resourceRoot)

You can aim specific players by passing a table as first parameter (or loop).

By the way be careful with your addEventHandler("onResourceStart", ...). Set getRootElement() (same as root predefined variable) will have for result to trigger the function everytime a resource is starting. You should rather use resourceRoot (for the actual resource).

addEventHandler("onResourceStart", resourceRoot, createMinigunWeapon)

 

ok thanks! 

Link to comment

It's also important to note the server script usually starts before the client script is ready (especially when the client needs to download the scripts and files), meaning an client-bound event dispatched on onResourceStart can turn up ignored for not being added client-side (as the client script isn't running yet). Best way to correct that is have the client send an event to the server when it is ready (i.e. when the resource starts client-side, after all necessary downloads -- onClientResourceStart), and the server's event handler for that event to triggerClientEvent back.

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