Noah_Antilles Posted May 21, 2017 Share Posted May 21, 2017 Hello everyone. After loads of work and help from you guys I finally managed to get a custom weapon script on the Hydra working... ...only to find out it doesn't sync to any other player in the server. Other players don't see the weapon. Other players don't get damaged by the weapon Vehicles the other player used aren't receiving any damage either. I tried to somehow get it to work this weekend, But I failed and I have no idea what to do. function hydraFunctions() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then --outputChatBox ("Hydra guns installed") toggleControl ( "accelerate", true ) toggleControl ( "brake_reverse", true ) toggleControl ( "vehicle_secondary_fire", false ) toggleControl ( "vehicle_fire", false ) setControlState ( "special_control_up", true) toggleControl ( "special_control_down", false) local x, y, z = getElementPosition(vehicle) local weaponR = createWeapon("m4", x, y, z) local weaponL = createWeapon("m4", x, y, z) local weaponHydraR = setElementData(vehicle, "weaponHydraR", weaponR, true) local weaponHydraL = setElementData(vehicle, "weaponHydraL", weaponL, true) attachElements ( weaponR, vehicle, -0.5, 5, -0.5, 0, 0, 90) attachElements ( weaponL, vehicle, 0.5, 5, -0.5, 0, 0, 90) setWeaponClipAmmo(weaponR, 5000000) setWeaponFiringRate(weaponR, 50) --setWeaponFlags(weaponR, "disable_model", true) setWeaponClipAmmo(weaponL, 5000000) setWeaponFiringRate(weaponL, 50) --setWeaponFlags(weaponL, "disable_model", true) end end end addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions ) function hydraFiring() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then if getElementData(vehicle, "weaponHydraL", true ) then setWeaponState(getElementData(vehicle, "weaponHydraR"), "firing") setWeaponState(getElementData(vehicle, "weaponHydraL"), "firing") --outputChatBox ("guns activated") end end end end end function hydraStopping() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then if getElementData(vehicle, "weaponHydraL", true ) then setWeaponState(getElementData(vehicle, "weaponHydraR"), "ready") setWeaponState(getElementData(vehicle, "weaponHydraL"), "ready") --outputChatBox ("guns deactivated") end end end end end bindKey("mouse1", "down", hydraFiring) bindKey("mouse1", "up", hydraStopping) bindKey("lctrl", "down", hydraFiring) bindKey("lctrl", "up", hydraStopping) How do I get this script to sync for all other players? so they can see the weapon too and get damaged by it (including the vehicle they're in) Link to comment
Moderators IIYAMA Posted May 21, 2017 Moderators Share Posted May 21, 2017 (edited) First of all you have to make a choice: Do you you want to use serverside as support for security and data validation? If not, then here are some reasons why the code isn't synchronized correctly and also how to counter those problems. You can't share client created elements with elementdata. To counter this you have to create for every client those weapons and set elementdata at the vehicle instead. So setElementData(vehicle, "weaponHydraL", weapon, false) -- disable the synchronization, else you will get trouble. You attach those weapons at the moment the vehicle gets streamed in or when it is already streamed in. There are events and functions for that. Sync weapon fire isn't working because of the first reason. setElementData(vehicle, "weaponFire", true, true) -- do sync this and catch up with: https://wiki.multitheftauto.com/wiki/OnClientElementDataChange > setWeaponState Keep debugging: Edited May 21, 2017 by IIYAMA Link to comment
Noah_Antilles Posted May 22, 2017 Author Share Posted May 22, 2017 Thanks for your reply. Well I guess I do want to make a serverside script, but I've never made one before, so that's quite a challenge I have no idea where to begin honestly. could you give me some more info where to start: How can I make two scripts (one serverside & one clientside) work together? As far as I can see the setElementData causes most problems. Is there something else I can use? Thanks for your time Link to comment
koragg Posted May 22, 2017 Share Posted May 22, 2017 (edited) You can use https://wiki.multitheftauto.com/wiki/TriggerClientEvent and https://wiki.multitheftauto.com/wiki/TriggerServerEvent to communicate between server and client scripts. Element data is pretty useful but not everytime so you just need to know when to use what Quote you have to create for every client those weapons This^ can be done with the "triggerClientEvent" thing and tell the resource to load the same stuff to every player on the server. Edited May 22, 2017 by koragg 1 Link to comment
Moderators IIYAMA Posted May 22, 2017 Moderators Share Posted May 22, 2017 (edited) The concept of letting serverside and clientside work together isn't very difficult. It actually the same as you and me having a conversation. You communicate with me that you have a problem with a script. Noah > IIYAMA Then I reply on your topic and gave you some tips. IIYAMA > Noah Then you process those tips and send me request to send you more information. Noah > IIYAMA Which I am sending you back now. IIYAMA > Noah During this conversation you have send me 2 messages and I have send you 2 messages back. @koragg showed you the functions you can use to start a conversation. Lets start a conversation between the Server and all clients! So a part of the conversation goes like this: (the rest of the conditions you have to fill in by yourself) Server: A lonely player named <John> enters a lonely vehicle. 'onVehicleEnter' gets triggered on serverside. Lets give the vehicle weapon so it can kill other lonely players!!! The server saves that the vehicle has a weapon now(for example in to a table). Oh great it has been saved! Now let all lonely players know that a vehicle has super weapons! triggerClientEvent to all the players. Clients: Oh man we all got mail from the server!! It said that a lonely vehicle has weapons now!! Now we all have to attach weapons to it on all our computers! Noooo!! This sucks damn. setElementParent(weapon, vehicle) Client: The lonely player named <John> pushed his left mouse button down! Yes, I am an a vehicle. Yes, it is a hydra. Yes, I am happy to be alive. Server, I am firing up!!! setElementData on his vehicle firing true. Clients: I received element:~yData oh oh. It said that a lonely hydra is firing fire! Which weapons did I attach on it? Ah wait I added them a few seconds ago, when a lonely player entered his lonely vehicle! Lets fire these hydra flame weapons! setWeaponState firing true Server: A lonely player named <John> crashed his hydra into his mother. There was nothing left of his mother, neither of his poor hydra dragon. setElementData on his vehicle firing false. Clients: I received element:~yData oh oh. It said that a lonely hydra isn't firing any more. What could have happened? setWeaponState firing false. Edited May 22, 2017 by IIYAMA 2 Link to comment
Noah_Antilles Posted May 23, 2017 Author Share Posted May 23, 2017 (edited) Thank you for your wonderful replies with explanation ^^ I will try my best to fix it Edited May 23, 2017 by Noah_Antilles Link to comment
Moderators IIYAMA Posted May 23, 2017 Moderators Share Posted May 23, 2017 I could have written it better. I skipped here and there some words, but I am happy that you like it. P.s: Learning tables will make you a god at scripting systems like this. 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