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

How would you create a datastore on every player that joins?

Asked by
Scout9 17
7 years ago

I'm looking to make a sort of points system for my warclan which will include DataStore, but there's 1 question. I want to save points per player, like the 'high ranks' can give players points, and I already have that sorted out. But then my question is, how would I make a datastore for every player that joins the game? Or is there an better way to organize this? I honestly don't know where to start.

1 answer

Log in to vote
1
Answered by 7 years ago
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerInfo") --Make the data store
game:GetService('Players').PlayerAdded:Connect(function(player) -- Once a player joins
    local Data = ("Data" .. player.UserId)  --Make like a tab for every player, under the same datastore
    local DataTable = DataStore:GetAsync(Data) or {0,1,20} -- Get their info, or make a list if they have no info already

    local stats= Instance.new('IntValue', player)  -- the rest of this block of code is just general stuff
    stats.Name = 'leaderstats'

    local Cash = Instance.new('IntValue', stats)
    Cash.Name = 'Cash'
    Cash.Value = DataTable[1] -- load the "Cash" that starts at 0 (Just an example)

    local Level = Instance.new('IntValue', stats)
    Level.Name = 'Lvl'
    Level.Value = DataTable[2] -- load the "Lvl" that starts at 1 (Just an example)

    local Thing = Instance.new('IntValue', stats)
    Thing.Name = 'Example?'
    Thing.Value = DataTable[3] -- load another item that starts at 20 (Just an example)
end)

--The example above will make the leaderstats, and retrieve any saved data. You still need to save the data though:


game.Players.PlayerRemoving:Connect(function(player) -- Once they're leaving
    --Code that retrieves the data, puts it in a list and saves the list to the datastore
end)

game:BindToClose(function() -- if the last player in the game leaves, don't close the server down until it's saved
    local p = game.Players:GetPlayers()
    for i=1,#p  do
        local player = p[i]
        --Execute code that does the same thing
    end
end)

I tried making it easy to understand how to set up some simple leader stats, it's kind of what I use but I have some more complex datastores going on. Either way, I hope I helped!

Ad

Answer this question