Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to save a list of strings for later viewing?

Asked by 7 years ago
Edited 7 years ago

I want to keep track of players who join my game, and more specifically who is joining multiple times (For now just a list that logs each player). I have been cruising the wiki in search of a way to do this, but I can't find anything apart from a page on saving player stats (Which isn't what I really want).

This is much more complex than anything I have tried, but i've decided I want to learn how. I don't necessarily want an example script (It would be helpful, but it would be cool to learn for myself.)

A wiki link or youtube video link will suffice, I am just lost on where to look.

I know how i can get the players name from when they join and such, but I mostly want to know how I can save and view this list of all players.

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

This should work really well: Google Analytics, the part about Adding your own events is probably of interest to you.

Plus with Google Analytics you get nice charts and graphs!

1
Neat :D Goulstem 8144 — 7y
Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 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.
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.

game.Players.PlayerRemoving:connect(function(plr) --When the player leaves
    local data = ds:GetAsync(plr.UserId) --Check for saved data
    if data then --If there is data
        local new = data + 1; --Increment it.
        ds:SetAsync(plr.UserId,new); --Save the incremented value
        print(plr.Name.." has joined "..new.." times!");
    else --If there isn't data
        ds:SetAsync(plr.UserId,1) --Save '1' for the 1st join.
        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!

Answer this question