Jump to content

Variables Client/Server


Recommended Posts

The case looks like this, I have a variable (Let's call it Ammo) that IS TO be in the giveWeapon function (source, 22, Ammo, true) but the problem is that this Ammo variable has the script type "client" and the function is the server script type, so how can I make this variable in this function? If someone has not understood what is going on, there is still a code to simplify...

 

-- Client side
local Ammo = 60
local AmmoMarker = createMarker(0, 0, 1, "cylinder", 2.0)
local WeaponMarker = createMarker(0, 0, 1, "cylinder", 2.0)

addEventHandler("onClientMarkerHit", AmmoMarker, function()
    Ammo = Ammo + 1
end)
addEventHandler("onClientMarkerHit", WeaponMarker, function()
    -- Here I get a weapon, the variable "Ammo" is the number of bullets
end)

-- Server Side

--??? Problem with importing variable "Ammo"

 

Link to comment
  • Moderators
8 minutes ago, redditing said:

I know but how?

 

Basic example: (but doesn't work very well with multiple players, since serverside is 1 environment and clientside an environment per player)

 

 triggerServerEvent ( "setAmmo", resourceRoot, Ammo)

 


addEvent("setAmmo", true)
addEventHandler("setAmmo", resourceRoot, 
function (Ammo_) 
	Ammo = Ammo_
end, false)

 

Edited by IIYAMA
  • Like 1
Link to comment
  • Moderators
9 minutes ago, redditing said:

Ok i understand thx

If you find it difficult to work with the event system, you can also try this out:

Example:

Spoiler

 Before you can use  the code below

 

The same as the example:

 

Client


callServer("setAmmo", Ammo)

 

Server


function setAmmo(Ammo_) 
	Ammo = Ammo_
end

 


 

Enhanced with callback:

 

Client


callServer("setAmmo", Ammo, 
function (Ammo_) 
  outputChatBox("Ammo has been set to " .. tostring(Ammo_) .. " on serverside")
end)

 

Server


function setAmmo(Ammo_) 
	Ammo = Ammo_

	return Ammo_
end

 

 

 

Edited by IIYAMA
  • Like 1
Link to comment

Okay, I have one problem, when I get into this "cylinder" then it gives me a pistol but with 30 bullets despite the fact that "Ammo" has a value of 72 Can you help me with that?

 

-- Client side
addEvent("WeaponGetAmmoToServer", true)

local Ammo = 72
local AmmoMarker = createMarker(0, 0, 1, "cylinder", 2.0)
local WeaponMarker = createMarker(0, 0, 1, "cylinder", 2.0)

addEventHandler("onClientMarkerHit", AmmoMarker, function()
    Ammo = Ammo + 1
end)
addEventHandler("onClientMarkerHit", WeaponMarker, function()
    addEventHandler("WeaponGetAmmoToServer", root, function()
        return Ammo
      end)
    
    triggerServerEvent("GiveWeaponToPlayer", root)
end)

-- Server Side

addEvent("GiveWeaponToPlayer", true)

addEventHandler("GiveWeaponToPlayer", root, function(prl)
    	local prl = client
    	giveWeapon(prl, 22, triggerClientEvent("WeaponGetAmmoToServer", source), true)
    end)

This script return Colt45 with 30 Ammo, but i want 72 from variable

Link to comment
  • Moderators
33 minutes ago, redditing said:

giveWeapon(prl, 22, triggerClientEvent("WeaponGetAmmoToServer", source), true)

You can't use triggerClientEvent to return values. Only MTA-Communication-Enchantment can do that with a callback, like this:

Spoiler

 

Client


function getPlayerCustomAmmo ()
	return Ammo
end

 

Server


addEvent("GiveWeaponToPlayer", true)
addEventHandler("GiveWeaponToPlayer", root, function(prl)
    	local prl = client

	callClient(prl, "getPlayerCustomAmmo", 
	function (Ammo_)   
		giveWeapon(client, 22, Ammo_, true)
	end)
    
end)


 

 

And I am not even sure that you need to do that, you can already provide that information on your first triggerServerEvent.

triggerServerEvent("GiveWeaponToPlayer", root, Ammo)

 

addEvent("GiveWeaponToPlayer", true)

addEventHandler("GiveWeaponToPlayer", root, function(Ammo_)
	local prl = client
	giveWeapon(prl, 22, Ammo_, true)
end)

 

 

 

Link to comment
53 minutes ago, IIYAMA said:

You can't use triggerClientEvent to return values. Only MTA-Communication-Enchantment can do that with a callback, like this:

  Reveal hidden contents

 

Client



 
function getPlayerCustomAmmo ()
	return Ammo
end

 

Server



 
addEvent("GiveWeaponToPlayer", true)
addEventHandler("GiveWeaponToPlayer", root, function(prl)
    	local prl = client

	callClient(prl, "getPlayerCustomAmmo", 
	function (Ammo_)   
		giveWeapon(client, 22, Ammo_, true)
	end)
    
end)

 

 

 

 

 

And I am not even sure that you need to do that, you can already provide that information on your first triggerServerEvent.


triggerServerEvent("GiveWeaponToPlayer", root, Ammo)

 


addEvent("GiveWeaponToPlayer", true)

addEventHandler("GiveWeaponToPlayer", root, function(Ammo_)
	local prl = client
	giveWeapon(prl, 22, Ammo_, true)
end)

 

 

 

 

98/5000
So I have a question (hypothetical question) can I return the value using "callServer", "callClient"?
Link to comment
  • Moderators
11 minutes ago, redditing said:

So I have a question (hypothetical question) can I return the value using "callServer", "callClient"?

yes, but only to a callback function as you can see in the example.

callClient(prl, "getPlayerCustomAmmo", function (Ammo_) end )

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