I’m trying to make a data store to save the settings in my game, I don’t wanna send too many data store requests though. This is the datastore script I tried but it wont save, is there a better way of doing this?
I also get this error message: line 33, attempt to index boolean with 'lasers'
local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Settings")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "settings"
folder.Parent = player
local lasers = Instance.new("BoolValue")
lasers.Name = "Lasers"
lasers.Parent = folder
local muzzleFlash = Instance.new("BoolValue")
muzzleFlash.Name = "MuzzleFlash"
muzzleFlash.Parent = folder
local gunLight = Instance.new("BoolValue")
gunLight.Name = "GunLight"
gunLight.Parent = folder
local data
local success, errorMessage = pcall(function()
data = ds:GetAsync(player.UserId.."-settings") or true
end)
if success then
print("Successfully loaded Settings data")
else
warn(tostring("Error loading Settings data: "..errorMessage))
end
if data then
lasers.Value = data.lasers
muzzleFlash.Value = data.muzzleFlash
gunLight.Value = data.gunLight
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
lasers = player.settings.Lasers.Value;
muzzleFlash = player.settings.MuzzleFlash.Value;
gunLight = player.settings.GunLight.Value
}
local success, errorMessage = pcall(function()
ds:SetAsync(player.UserId, data)
end)
if success then
print("Settings data successfully saved")
else
warn(tostring("Error saving Settings data: "..errorMessage))
end
end)
game:BindToClose(function()
print("Game is closing")
wait(3)
print("Game is closed")
end)