Jump to content

Use a table for player data


Dudalus

Recommended Posts

Hi

I use element data's for saving my playerstats, but the problem is, that they aren't safe (Clienthack), so my plan is to use a table.

My first idea was to block all (player-element) changes from from client to the server (onElementDataChange) and use an own setPlayerElementData function. Before I use a clientside getElementData function , I sync the data from the server. But this solution has an overhead, and tables look much better

Is there a good way to use a table ?

Link to comment

Tables are a very efficent way to store data :), I use to them in many of my scripts found in K@N Zombies. This is a small example I cooked up for you.

playerSkin = {} 
  
addEventHandler("onPlayerWasted", getRootElement(), 
function() 
    playerSkin[source] = getElementModel(source) 
end ) 
  
addEventHandler("onPlayerSpawn", getRootElement(), 
function() 
    setElementModel(source, playerSkin[source] or 0) 
end ) 

Its very simple, playerSkin[source] is adding a key to the table so and by using it like a variable its setting a value to the key.

playerSkin[source] == getElementModel(source) is equivalent to

playerSkin = { playerElement = skinID  } 

I hope this helps you understand, if it doesnt please explain what you are planning to do and the community of MTASA will help you :).

Edited by Guest
Link to comment
Tables are a very efficent way to store data :), I use to them in many of my scripts found in K@N Zombies. This is a small example I cooked up for you.
playerSkin = {} 
  
addEventHandler("onPlayerWasted", getRootElement(), 
function() 
    playerSkin[source] == getElementModel(source) 
end ) 
  
addEventHandler("onPlayerSpawn", getRootElement(), 
function() 
    setElementModel(source, playerSkin[source]) 
end ) 

Its very simple, playerSkin[source] is adding a key to the table so and by using it like a variable its setting a value to the key.

playerSkin[source] == getElementModel(source) is equivalent to

playerSkin = { playerElement = skinID  } 

I hope this helps you understand, if it doesnt please explain what you are planning to do and the community of MTASA will help you :).

Your code won't work because two reasons:

- You're using a comparition symbol instead of a single =.

- In 'onPlayerSpawn' it won't work the first time as when the player is spawned for first time he hasn't been killed yet.

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