Jump to content

createWeapon sync for all players


Recommended Posts

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

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 by IIYAMA
Link to comment

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

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 :P

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 by koragg
  • Like 1
Link to comment
  • Moderators

The concept of letting serverside and clientside work together isn't very difficult.

It actually the same as you and me having a conversation.

 

  1. You communicate with me that you have a problem with a script. Noah > IIYAMA
  2. Then I reply on your topic and gave you some tips. IIYAMA > Noah
  3. Then you process those tips and send me request to send you more information. Noah > IIYAMA
  4. 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)

  1. 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.
  2. 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)
  3. 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.
  4. 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
  5. 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.
  6. 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 by IIYAMA
  • Like 2
Link to comment
  • Moderators

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

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