Answered by
8 years ago Edited 8 years ago
What you need
What you are looking for is DataStores.
DataStores act like a giant table consisting of keys and values saved for the server(or game place). You may save any and all data you want with the exception of instances.
Setting it up
So, lets start with how you would set your datastores up.
- 1) The
GetDataStore
function of DataStoreService will allow you to setup a new datastore. This takes in a string as an argument. If the string is changed, you will have a different DataStore.
1 | local ds = game:GetService( "DataStoreService" ):GetDataStore( "PlayersEntered" ); |
Saving and retrieving your data
- 2) Now you have to think of what you're trying to save - the names of players who entered your game, and how many times they have joined. You can use the
SetAsync
function to assign a value to a key inside your datastores. This value can be retrieved later by the provided key, using GetAsync
.
Using the PlayerRemoving
event, you can check if the player that is leaving has any data stored already. If not, save a value of 1 to their UserId. This will identify the data as theirs. If there is data stored already, simply increment it and save it again.
01 | game.Players.PlayerRemoving:connect( function (plr) |
02 | local data = ds:GetAsync(plr.UserId) |
05 | ds:SetAsync(plr.UserId,new); |
06 | print (plr.Name.. " has joined " ..new.. " times!" ); |
08 | ds:SetAsync(plr.UserId, 1 ) |
09 | print (plr.Name.. " joined the server once" ); |
Alternative methods
Note: You may also use the IncrementAsync
function for this, since all your values are going to be integers. This would look like;
1 | ds:IncrementAsync(plr.UserId, 1 ); |
Hope I helped! And happy developing!