New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
-1

How would I structure a Data Store?

Asked by 3 years ago

I plan on saving customizable user items in a data store, but my problem is how would I structure it?

Lets say there there are 20 types of bikes the user can customize in 7 different category's (Wheels, color, handle bars ETC. ), would I make 20 different data stores with 5 tables in them? Or would I make 1 datastore and somehow cram all the info into a table?

EDIT:

Bike format:

       |--wheels (7 options to pick from)
       |--paint (7 options to pick from)

bike 1 --| bars (7 options to pick from)

There are 20 different types of bikes total. How would I save all that information. Multiple data stores?

1 answer

Log in to vote
1
Answered by 3 years ago

Since datastores can save tables, this is very easy.

local ds = game:GetService("DataStoreService"):GetDataStore("DataStoreName")
local defaultItems = { -- this will be what the player gets if they're new to the game
"AwesomeHat", -- idk some random item name
"CoolBeans", -- another random one
-- add more of the items you want
}
local data = {} -- where all the players data is stored for everyone in the server

game.Players.PlayerAdded:connect(function(plr)
local tab = ds:GetAsync(plr.userId)
if not tab then tab = defaultItems end -- if they're a new player set to the default table
local found = false

for i = 1,#data do -- makes it so multiple data's aren't stored in the data tab
if data[i][1] == plr.Name then
found = true
break
end
end

if found == false then
table.insert(data,{plr.Name,tab})
end

end)

game.Players.PlayerRemoving:connect(function(plr)
local tab = nil
for i = 1,#data do
if data[i][1] == plr.Name then
tab = data[i][2]
brek
end
end

ds:SetAsync(plr.userId,tab) -- saves their data
end)

Hopefully that helps a bit..

0
I dont think you understood the question. One bike can be customized in several ways. I want all 20 bikes to have all their features saved the way the player set them to. Orlando777 315 — 3y
0
That's what the tables for. It saves their features and can be loaded the next time it's needed... ObscureEntity 294 — 3y
0
I edited the question. I know what tables do. But technically speaking I would have to put a table in a table. Is it possible to just create multiple data stores? Orlando777 315 — 3y
0
if you want different bikes then you can just save other tables inside that table ObscureEntity 294 — 3y
View all comments (4 more)
0
local tab = { {"Bike1",{"Bikeattachment1","BikeAttachment2"}},{"Bike2",{"BikeAttachment1",BikeAttachment2}}} ObscureEntity 294 — 3y
0
Btw, that doesnt seem to work. I cant seem to access Bikeattachment1 or 2, it says its nil. Orlando777 315 — 3y
0
it would be: local tab = {Bike1 = {"Bikeattachment1","BikeAttachment2"},Bike2 = {"Bikeattachment1","BikeAttachment2"}} Orlando777 315 — 3y
0
you can't do that with datastores. You can't do things like Bike1 = hdsgkjasdhsd. To get the data it's like tab[1][1] to get bike attachment 1. ObscureEntity 294 — 3y
Ad

Answer this question