Jump to content

Use a table for player data


Dudalus

Recommended Posts

Posted

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 ?

Posted (edited)

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

"[...] If you don’t love it, if you’re not having fun doing it, you don’t really love it, you’re going to give up." - Steve Jobs, 2007

Posted

Hey, thank you for the reply, it works fine with tables. onPlayerSpawn isn't that problem, because I call (trigger) an own function ;)

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