Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.