Jump to content

[Doubt] Server Side


raynner

Recommended Posts

Hello everyone :P 

Good friends I have a little doubt I do scripts for mta 3 years ago is from the beginning I wonder one thing about the server side.

let's start, we all know that variables used on the server side have their value shared with the whole server.

so I can dribble this problem I always create a table for each player so that I can set a variable with a separate value for each player plus the times I feel I do not need to do this anymore I'm not sure, keep reading.

I know that Moon performs functions in an ordered way eg if two players call an execution of the same function according to my knowledge on the moon it should run the function twice, once for each right player? and here comes my doubt if I need to make a local variable that loses its value at the end of the function I can simply define a variable or I have to define it inside a table for each player. because although he gave to know how it works, I'm not sure if this gives me my doubts. the function is actually executed 1 time and as soon as it finishes it is executed again for the next player or it is performed doubly at the same time :? . I know this seems like an unnecessary question more is how I said my fear is by the value of the variables. Thanks Friends :D 

excuse me for English evil

Link to comment
  • Moderators

I don't know exactly what you mean by "the variable is shared with the whole server", but just to clarify: A local variable is limited only to the script whereas a global variable is limited to the resource itself.

I read the other part of your post like 5 times but unfortunately I didn't understand what you meant. I suggest you illustrate your question using a code.

 

PS: When translating something to English, be careful about the Portuguese word "Lua" which is moon in English.

Link to comment
addEventHandler("onPlayerLogin", root,
function()
	local Verific = true
	for i,v in ipairs(dbPoll(dbQuery(dbPlayer, "SELECT account FROM player"), -1)) do
		if tostring(v["account"]) == getAccountName(getPlayerAccount(source)) then
			Verific = false
			break
		end
	end
	if Verific then
		dbExec(dbPlayer, "INSERT INTO player DEFAULT VALUES")
		dbExec(dbPlayer, "UPDATE player SET account = ? WHERE ID = ?", getAccountName(getPlayerAccount(source)), getPlayerID())
	end
	Verific = true
	UpdateData(source)
end)

see the variable "Verific" what I'm wondering is that every variable created on the server side has its equal value for all players.
what I'm wondering is see this function if two players trigger the "onPlayerLogin" event at the same time. this function would be executed twice at the same time or it would run two times one after the other, you see?

I know that Moon works the second way more despite mta using the language Lua I believe it does not work the same way! ie if this function is executed twice at the same time my variable (let's say with global value since the value is the same for all players) would not work as I planned and would have to do otherwise.

all I want to know is if the client side functions are executed multiple times at the same time or one after the other (assuming it is called several times consecutively) this is my only question.

Edited by raynner
Link to comment
  • Moderators

onPlayerLogin is triggered when each player logs in and it gets executed for all players.

About the variable, it's accessible only inside the event and this code looks fine from what I can see.

59 minutes ago, raynner said:

all I want to know is if the client side functions are executed multiple times at the same time or one after the other (assuming it is called several times consecutively) this is my only question.

Client-side functions works individually while the Server-side functions works globally, ie for all players at the same time.

Edited by DNL291
Link to comment

Do not you understand, you answered what I already know! I am talking about how the function is executed if it is called 2 times then it will be executed twice at the same time or it will be executed once and when it reaches the end of the block it will be executed again :S

Link to comment
  • Moderators
7 minutes ago, raynner said:

Do not you understand, you answered what I already know! I am talking about how the function is executed if it is called 2 times then it will be executed twice at the same time or it will be executed once and when it reaches the end of the block it will be executed again :S

Relax, no need to act like a mentally unbalanced person.

I'm sure I got what you said. I think you weren't able to understand how everything works. If you read carefully what I have explained, you will know that it should be executed like in the second way: 

14 minutes ago, raynner said:

it will be executed once and when it reaches the end of the block it will be executed again 

 

Link to comment
  • Moderators

Here's an example if you haven't understand yet:

addEvent( "onPlayerWasted2" )

addEventHandler( "onPlayerWasted", root,
	function ( totalAmmo, killer )
		triggerEvent( "onPlayerWasted2", source, killer )
	end
)

addEventHandler( "onPlayerWasted2", root,
	function ( killer )
		outputChatBox( "hasKiller: "..tostring(hasKiller) )
		-- output: "hasKiller: nil"
		local hasKiller = false
		if killer and isElement(killer) then
			hasKiller = true
		end
	end
)

I've used a custom event so you can undestand with more ease how the event is called.

Note that "onPlayerWasted2" won't get called at the same funcion for two players, so the variable "resets" each time the event is called.

Edited by DNL291
Link to comment
3 hours ago, DNL291 said:

Here's an example if you haven't understand yet:


addEvent( "onPlayerWasted2" )

addEventHandler( "onPlayerWasted", root,
	function ( totalAmmo, killer )
		triggerEvent( "onPlayerWasted2", source, killer )
	end
)

addEventHandler( "onPlayerWasted2", root,
	function ( killer )
		outputChatBox( "hasKiller: "..tostring(hasKiller) )
		-- output: "hasKiller: nil"
		local hasKiller = false
		if killer and isElement(killer) then
			hasKiller = true
		end
	end
)

I've used a custom event so you can undestand with more ease how the event is called.

Note that "onPlayerWasted2" won't get called at the same funcion for two players, so the variable "resets" each time the event is called.

Friend I know how it works I just had doubts about how the machine performed the function!

Link to comment
  • Moderators

raynner, I think you're contradicting yourself and so far you haven't been able to formulate your question correctly.

On 16/02/2018 at 21:52, raynner said:

see the variable "Verific" what I'm wondering is that every variable created on the server side has its equal value for all players.
what I'm wondering is see this function if two players trigger the "onPlayerLogin" event at the same time. this function would be executed twice at the same time or it would run two times one after the other, you see?

I know that Moon Lua works the second way

Why the hell did you ask that question if you answered it yourself?

On 16/02/2018 at 21:52, raynner said:

despite mta using the language Lua I believe it does not work the same way! ie if this function is executed twice at the same time my variable (let's say with global value since the value is the same for all players) would not work as I planned and would have to do otherwise.

If by "executed twice at the same time" you mean the function being called 2 times then you can see the example I posted above. If you think you're having some kind of problem with the variable, then show us how it's happening so we can help you fix that.

On 17/02/2018 at 22:06, raynner said:

Friend I know how it works I just had doubts about how the machine performed the function!

Explain what exactly you want to know about the function execution process.

Edited by DNL291
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...