BenceDev Posted June 6, 2022 Share Posted June 6, 2022 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
Mkl Posted June 6, 2022 Share Posted June 6, 2022 (edited) 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 June 6, 2022 by Mkl 1 Link to comment
BenceDev Posted June 6, 2022 Author Share Posted June 6, 2022 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
Addlibs Posted June 7, 2022 Share Posted June 7, 2022 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now