Apologies. Since I don’t use the DataStoreService() often, I’m not too familiar with it.
However, I have spotted an error to your code. Since accessing a DataStore is a network request, it can throw errors. Make sure to always use a pcall() when reading from or writing to it.
if VehicleData then
local Cars = {
vehiclename,
}
for i,v in VehicleData do
Cars[i]=v
end
VehicleStorage:SetAsync(player.UserId, Cars)
else
local Cars = {
vehiclename,
}
VehicleStorage:SetAsync(player.UserId, Cars)
end
I wasn’t utilizing the vehicle example but I did this:
local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("StoreNEW")
local function checkData(player)
local data = store:GetAsync(player.Name)
for i,v in pairs(data) do
print(v)
print(i)
print(v.Officer)
end
end
local function addData(player)
local thing = {
["Arrest"] = {
Officer = "Officer"
}
}
store:SetAsync(player.Name, thing)
end
game.Players.PlayerAdded:Connect(function(player)
addData(player)
local VehicleData = store:GetAsync(player.Name)
if VehicleData then
local Cars = {
["Arrest"] = {
Officer = "NewOfficer"
},
}
for i,v in VehicleData do
print(v)
print(Cars[i])
Cars[i]=v
end
store:SetAsync(player.UserId, Cars)
warn("editing")
else
local Cars = {
["Arrest"] = {
Officer = "Officer"
}
,
}
store:SetAsync(player.UserId, Cars)
end
checkData(player)
end)
local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("StoreNEW")
local function checkData(player)
local data = store:GetAsync(player.Name)
for i,v in pairs(data.Arrest) do
print(v) --value, every each officer that arrested this person
print(i) --index
end
end
local function addData(player)
local thing = {
["Arrest"] = {
"Officer"
}
}
store:SetAsync(player.Name, thing)
end
game.Players.PlayerAdded:Connect(function(player)
addData(player)
local VehicleData = store:GetAsync(player.Name)
if VehicleData then
local Cars = {
["Arrest"] = {
"NewOfficer"
},
}
for i,v in VehicleData.Arrest do
if table.find(Cars.Arrest,v) then --avoid duplication
continue
end
table.insert(Cars.Arrest,v)
end
store:SetAsync(player.UserId, Cars)
warn("editing")
else
local Cars = {
["Arrest"] = {
"Officer"
},
}
store:SetAsync(player.UserId, Cars)
end
checkData(player)
end)
From the checkData() function I should get the index of 1 and 2 because I am adding one from the addData() function and then a further one when they join. But I am only receiving one data from the store.
I’m also wanting to add more information into the values so it will eventually be:
Try setting async when a player leaves, not when the value gets larger/smaller. You can also try to read the saved data then add 1 and saving it. Using LoadAsync, you can add it to a variable and save that + 1
I don’t want to set the Async when they leave. I am only using PlayerAdded so I can test this. And adding 1 isn’t what I mean, the index should be 2, 1 and 2 because there should be 2 values in the DataStore for that Player as for testing they have 2 arrests.
You still don’t understand, saving data when player leaves is the solution but then you have to get previous data and store it on server until player leaves, use modules as i told, it will allow you to have your dictionary for player from joining to leaving
I would prefer if I used a method I have used previously and what I know more on rather than go straight into using ModuleScripts which I have barely used especially for using it to save Data as a method. The only difficulty I am having is figuring out how to add NEW data onto a PLAYERS data without it clearing it all. So the Players Data will consist of the previous arrests and the new arrest.
Why would I save it when the player leaves when it will be just one FireServer() event to the server to create the arrest? It isn’t depending on when the player leaves, it will be a RemoteEvent and when it is Fired it will create the arrest on the player.
I’ve made a possible solution to my problem based on @Bakonowychlopak123’s response.
local dss = game:GetService('DataStoreService')
local store = dss:GetDataStore("Store_Name")
local function checkData(player)
for i,v in pairs(store:GetAsync(player.Name)) do
print(i) -- check to make sure its working
print(v)
end
end
game.Players.PlayerAdded:Connect(function(player)
local data = store:GetAsync(player.Name)
if data then -- if player already has existing data then
local cars = {
["Arrest"] = {
Type = "Arrest",
Officer = "example"
}
}
for i,v in pairs(data) do
cars[i] = {
Type = v.Type,
Officer = v.Officer
}
end -- loop through all of the players data and add its values to the "cars" table
store:SetAsync(player.Name, cars) -- set players data to the "cars" table
else
store:SetAsync(player.Name, {
["Arrest1"] = { -- change name so there's no duplication (use a random code generator)
Type = "Arrest",
Officer = "TEST"
}
})
end
checkData(player) -- check it worked correctly
end)
I gotcha, but I just find my method way easier for me because it’s less complicated and I don’t use ModuleScripts, nevermind even using it to save a players data.
xd, still i tell you don’t save data to module script but use module script as table, but ok, if it’s easier for you it’s nice, i had problems with this too one day soo yea good luck with making project :}