Hello there, im writing a car script and i wanna save lots of cars. A value changes to owned when bought so you can always save cars. It works but saving doenst, i get the warning too many data store requests, all further requests will be dropped etc. Could anybody help? Thank you!
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("Car1")
local ds1 = DataStore:GetDataStore("Car2")
local ds2 = DataStore:GetDataStore("Car3")
local ds3 = DataStore:GetDataStore("Car4")
local ds4 = DataStore:GetDataStore("Car5")
local ds5 = DataStore:GetDataStore("Car6")
local ds6 = DataStore:GetDataStore("Car7")
local ds7 = DataStore:GetDataStore("Car8")
local ds8 = DataStore:GetDataStore("Car9")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
leader.Name = "OwnedCars"
local Car = Instance.new("StringValue",leader)
Car.Name = "Car1"
Car.Value = ds:GetAsync(player.UserId)
Car.Parent = player
ds:SetAsync(player.UserId, Car.Value)
Car.Changed:connect(function()
ds:SetAsync(player.UserId, Car.Value)
end)
local Car2 = Instance.new("StringValue",leader)
Car2.Name = "Car2"
Car2.Value = ds1:GetAsync(player.UserId)
Car2.Parent = player
ds1:SetAsync(player.UserId, Car2.Value)
Car2.Changed:connect(function()
ds1:SetAsync(player.UserId, Car2.Value)
end)
local Car3 = Instance.new("StringValue",leader)
Car3.Name = "Car3"
Car3.Value = ds2:GetAsync(player.UserId)
Car3.Parent = player
ds2:SetAsync(player.UserId, Car3.Value)
Car3.Changed:connect(function()
ds2:SetAsync(player.UserId, Car3.Value)
end)
local Car4 = Instance.new("StringValue",leader)
Car4.Name = "Car4"
Car4.Value = ds3:GetAsync(player.UserId)
Car4.Parent = player
ds3:SetAsync(player.UserId, Car4.Value)
Car4.Changed:connect(function()
ds3:SetAsync(player.UserId, Car4.Value)
end)
local Car5 = Instance.new("StringValue",leader)
Car5.Name = "Car5"
Car5.Value = ds4:GetAsync(player.UserId)
Car5.Parent = player
ds4:SetAsync(player.UserId, Car5.Value)
Car5.Changed:connect(function()
ds4:SetAsync(player.UserId, Car5.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.Car1.Value, player.Car2.Value, player.Car3.Value, player.Car4.Value, player.Car5.Value)
end)
Instead of having so many data stores, just use 1 data store, and make a dictationary storing all the information of the cars.
Im really bad at datastores, could you show a example script?
Roblox gives you 4MB for storage for each datastore key so unless you have to deal with huge amounts of information about a user you can store their data in a single datastore as a dictionary.
Basically, you need a way to convert what you want to save(settings, owned items, currencies) into a dictionary and then save it in a single datastore(for example the datastore named “UserData”, it can be anything) using a key(most games use the userId as the key or Player_userId) to write and read the data(imagine the datastore is a library, the key is the shelf and the books are the player data, for example, a book might be the cars they own). That way you can fetch and change the data with a single datastore API request instead of multiple ones(also there’re open-source libraries like Datastore2 and ProfileService that handle datastore errors for you and convert the entire thing into easy-to-use functions).
I think it might be because you’re passing too many requests when trying to save multiple values to your DataStore in “PlayerRemoving”.
In order to resolve this problem, you have the option of combining all the values into either a unified string or table and using it as the second parameter when invoking the SetAsync() function.
local DataStore = game:GetService("DataStoreService")
local carDataStore = DataStore:GetDataStore("OwnedCarsDataStore")
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder", player)
leader.Name = "OwnedCars"
for i = 1, 9 do -- Loops through the 9 possible car slots
local carName = "Car" .. i
local carValue = Instance.new("StringValue", leader) -- Creates a string value for them all
carValue.Name = carName
carValue.Value = carDataStore:GetAsync(player.UserId .. carName) or "unowned" -- Get the value from the DataStore
-- if no value from datastore, set to "unowned"
carValue.Parent = player
carValue.Changed:connect(function() -- Looked for a change in all of them using one .Changed instead of many
carDataStore:SetAsync(player.UserId .. carName, carValue.Value) -- Updates the DataStore with new vals
end)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local values = {} -- Table to hold values for each car
for i = 1, 9 do -- Loop through them again
local carValue = player.OwnedCars:FindFirstChild("Car" .. i) -- Get the value we created earlier
if carValue then
table.insert(values, carValue.Value) -- if player owns car, add it to their values table
end
end
if #values > 0 then -- Checks that the player has at least one car so we're not calling SetAsync to save nothing
carDataStore:SetAsync(player.UserId .. "AllCars", table.concat(values, ",")) -- table.concat updates the DataStore with a comma-separated list of their car names
end
end)
I added some comments to help you better understand what this code does and the changes made. Feel free to ask any questions!
local DataStore = game:GetService("CarDataStore")
local data = {
[1] = {
["Name"] = "car 1",
["Cost"] = "500"
},
[2] = {
["Name"] = "car 2",
["Cost"] = "500"
}
}
--save the data
Apologies if the formatting is unclear, I am on mobile.