-
What do you want to achieve? I want to fix a script that doesn’t work.
-
What is the issue? I tried to save multiple values into a table but they don’t save.
-
What solutions have you tried so far? I see that when I reset my character the values are still there saved but when I rejoin that is when the values in the folder are not turned on as last time.
If someone could find the reason I have this problem it would be great. I don’t know if its from the script so if you find the problem in the script tell me what change I should do!
local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local CarDataStore1 = dataStore:GetDataStore("VehicleDataStore0")
local defaultCarOwnership = {
["2020BloxWagenGulf"] = false,
["1983PIATDOS"] = false,
["2020TacoCarry"] = false,
["2015PiatHundred"] = false,
["2003JacuzziRS4"] = false,
["2020TokenModelIII(Uncustomizable)"] = false,
["1997ToySupra"] = false,
["2008BAM320Y"] = false,
}
function setCarOwnership(player, carName, ownership)
local folder = player:FindFirstChild("OwnedCars")
if not folder then
folder = Instance.new("Folder")
folder.Name = "OwnedCars"
folder.Parent = player
end
local car = folder:FindFirstChild(carName)
if not car then
car = Instance.new("BoolValue")
car.Name = carName
car.Parent = folder
end
car.Value = ownership
CarDataStore1:SetAsync(player.UserId .. "_" .. carName, ownership)
end
function getCarOwnership(player, carName)
local folder = player:FindFirstChild("OwnedCars")
if not folder then
return defaultCarOwnership[carName]
end
local car = folder:FindFirstChild(carName)
if not car then
return defaultCarOwnership[carName]
end
local value = CarDataStore1:GetAsync(player.UserId .. "_" .. carName)
if value ~= nil then
return value
else
return car.Value
end
end
players.PlayerAdded:Connect(function(player)
for carName, defaultOwnership in pairs(defaultCarOwnership) do
local ownership = getCarOwnership(player, carName)
setCarOwnership(player, carName, ownership)
end
end)
for _, player in ipairs(players:GetPlayers()) do
for carName, defaultOwnership in pairs(defaultCarOwnership) do
local ownership = getCarOwnership(player, carName)
setCarOwnership(player, carName, ownership)
end
end
players.PlayerRemoving:Connect(function(player)
CarDataStore1:RemoveAsync(player.UserId)
end)
game:BindToClose(function()
for _, player in ipairs(players:GetPlayers()) do
CarDataStore1:SetAsync(player.UserId, nil)
end
end)