Hello,
So I am doing a Open-World Type racing game, and was wondering how can I save multiple values into one database.
I have figured out the buying/spawning system of cars, and all works fine but the thing that I am stuck on is the color saving (since I am thinking of adding a customization garage where you can modify your car to the bolts). I have tried creating color DataStores for every single car that a player can buy, but since I plan to add a lot of cars, LIKE a looot of cars, I was thinking of just doing 1 DataStore named “PrimaryColor” and then saving “colorValues” (or intValues) into “PrimaryColor” each having a name of:
“CarName_ColorName”
Doing so, it will help me pull values from the datastore with ease for each specific car but how do I save them in a way that I can later pull them out, using only the name of the car?
You can save multiple values to a table, then you only need to save the table and it will save all values with it.
Example:
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("ExampleSave")
game.Players.PlayerAdded:Connect(function(Player)
if not Player:FindFirstChild("FolderName") then
local Folder = Instance.new("Folder" ,Player)
Folder.Name = "FolderName"
end
local Folder = Player:WaitForChild("FolderName" ,5)
local Value1 = Instance.new("IntValue",Folder)
local Value2 = Instance.new("IntValue",Folder)
Value1.Name = "Value1"
Value2.Name = "Value2"
---------------------------------------------------------
local GetData = ds:GetAsync(Player.UserId)
if GetData ~= nil then
Value1.Value = GetData[1]
Value2.Value = GetData[2]
else
Value1.Value = 0
Value2.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local SaveData = {}
for _,Child in pairs(Player.FolderName:GetChildren())do
table.insert(SaveData,Child.Value)
end
ds:SetAsync(Player.UserId, SaveData)
end)
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local [VarName] = Instance.new("Folder",player)
[VarName].Name = "leaderstats" -- //Change leaderstats to anything
local val1 = Instance.new("IntValue",[VarName])
val1.Name = "Val1" -- //Change to whatever
val1.Value = 0
local val2 = Instance.new("IntValue",[VarName])
val2.Name = "Val2"
val2.Value = 0
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
val1.Value = data
val2.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
myDataStore:SetAsync(player.UserId,{player.leaderstats.Val1.Value,player.leaderstats.Val2.Value})
end)
if success then
print("Success")
else
warn(err)
end
end)
You can make a dictionary instead of an array so you can quite literally pull a value out of the table using a string key if this is what you’re looking for.
Here is the code.
I also have another script that creates these values in the player’s folder (folder is located at game.Players).
--Values
local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local PrimaryColors = DataStore:GetDataStore("PrimaryColors")
local SecondaryColors = DataStore:GetDataStore("SecondaryColors")
local HttpService = game:GetService("HttpService")
players.PlayerAdded:Connect(function(player)
local ColorFolder = player:WaitForChild("HuracanColors")
local playerUserId = "Player_" .. player.UserId
local color_statsP = HttpService:JSONDecode(PrimaryColors)
local color_statsS = HttpService:JSONDecode(SecondaryColors)
for statName, val in pairs(ColorFolder:GetChildren()) do
if statName == "HuracanPColor" then
val.Value = color_statsP.HuracanPColor
end
if statName == "HuracanSColor" then
val.Value = color_statsS.HuracanSColor
end
end
print("loaded")
end)
players.PlayerRemoving:Connect(function(Player)
local Pcolor_stats = {}
local Scolor_stats = {}
local ColorFolder = Player:WaitForChild("HuracanColors")
for statName, val in pairs(ColorFolder:GetChildren()) do
if statName == "HuracanPColor" then
Pcolor_stats[val.Name] = val.Value
end
if statName == "HuracanSColor" then
Scolor_stats[val.Name] = val.Value
end
end
PrimaryColors:SetAsync(Player.UserId,HttpService:JSONEncode(Pcolor_stats))
SecondaryColors:SetAsync(Player.UserId,HttpService:JSONEncode(Scolor_stats))
wait(1)
print("Saved")
end)