local players = game:GetService('Players')
local Data = game:GetService("DataStoreService"):GetDataStore("CarSaveSystemDataSet")
players.PlayerAdded:Connect(function(player)
local cars = {
'Lykan HyperSport',
'Mustang',
'One:1',
}
local carFolder = Instance.new('Folder', player)
carFolder.Name = 'Cars'
for _,car in pairs(cars) do
local val = Instance.new('StringValue', carFolder)
val.Name = car
val.Value = 'Unowned'
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local p = {plr.Cars.Mustang.Value,plr.Cars['Lykan HyperSport'].Value,plr.Cars['One:1'].Value,}
Data:SetAsync(plr.userId, p)
print(plr.Name.."'s Data was saved.")
end)
If you could please help, that would mean a lot. It just wont DataStore properly, and the values wont save.
The function connected to players.PlayerRemoving will not fire when the game is shutting down. This would be best fixed using game:BindToClose and binding a short wait(3) or so to let the save take place. That’s for a hacky solution though…
There was another thread I helped with recently that was fleshing out the technicalities of this, I recommend you at least read the first paragraph and maybe even the solution:
You never call getAsync to load data. It saves, you never load it.
Try this:
players.PlayerAdded:Connect(function(player)
local cars = {
'Lykan HyperSport',
'Mustang',
'One:1',
}
local carFolder = Instance.new('Folder', player)
carFolder.Name = 'Cars'
local data = Data:GetAsync(plr.userId)
for i, car in pairs(cars) do
local val = Instance.new('StringValue', carFolder)
val.Name = car
val.Value = 'Unowned'
if data[i]~='Unowned' then
--Make owned
val.Value = 'Owned' --You might do it diferently, just a example.
end
end
end)
Yeah, I made a typo (mobile is not my favorite). It should work now.
Full code:
local players = game:GetService('Players')
local Data = game:GetService("DataStoreService"):GetDataStore("CarSaveSystemDataSet")
players.PlayerAdded:Connect(function(player)
local cars = {
'Lykan HyperSport',
'Mustang',
'One:1',
}
local carFolder = Instance.new('Folder', player)
carFolder.Name = 'Cars'
local data = Data:GetAsync(plr.userId)
print(plr.Name.."'s Data is loading...")
for i, car in pairs(cars) do
local val = Instance.new('StringValue', carFolder)
val.Name = car
val.Value = 'Unowned'
if data[i]~='Unowned' then
--Make owned
val.Value = 'Owned' --You might do it diferently, just a example.
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local p = {plr.Cars.Mustang.Value,plr.Cars['Lykan HyperSport'].Value,plr.Cars['One:1'].Value,}
Data:SetAsync(plr.userId, p)
print(plr.Name.."'s Data was saved.")
end)