##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.
- 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.
local ds = game:GetService("DataStoreService"):GetDataStore("PlayersEntered");
##Saving and retrieving your data
- 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.
game.Players.PlayerRemoving:connect(function(plr)
local data = ds:GetAsync(plr.UserId)
if data then
local new = data + 1;
ds:SetAsync(plr.UserId,new);
print(plr.Name.." has joined "..new.." times!");
else
ds:SetAsync(plr.UserId,1)
print(plr.Name.." joined the server once");
end
end)
##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;
ds:IncrementAsync(plr.UserId,1);
Hope I helped! And happy developing!
1 comment
Neat :D