I’ve tried so many thing, posted so many topic and nothing has worked. I’m at the last straw with this DataStore. I cannot figure out why the Particle.Enabled property and the Sound.Volume property are not saving. I’ve tried so many DevForum topics, free models, YouTube videos, nothing.
Game API is on
Script:
local DataStoreService = game:GetService("DataStoreService")
local SettingsDataStore = DataStoreService:GetDataStore("SettingsDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local ParticleData
local SoundData
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
ParticleData = Particle.Enabled
end
end
for i, Sound in pairs(game.SoundService:GetDescendants()) do
if Sound.Name ~= "BackgroundMusic" and Sound:IsA("Sound") then
SoundData = Sound.Volume
end
end
local Data
local DataToSave = {
ParticleData;
SoundData;
}
local Success, Error = pcall(function()
Data = SettingsDataStore:GetAsync(Player.UserId)
end)
if Success then
DataToSave[1] = Data[1]
DataToSave[2] = Data[2]
else
warn(Error)
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local ParticleData
local SoundData
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
ParticleData = Particle.Enabled
end
end
for i, Sound in pairs(game.SoundService:GetDescendants()) do
if Sound.Name ~= "BackgroundMusic" and Sound:IsA("Sound") then
SoundData = Sound.Volume
end
end
local DataToSave = {
ParticleData;
SoundData;
}
local Success, Error = pcall(function()
SettingsDataStore:SetAsync(Player.UserId, DataToSave)
end)
if Success then
print("Player data saved!")
else
warn(Error)
end
end)
DataToSave is not globally available as a variable. You’ll instead want to keep a table that keeps track of the data to save for each of the players, and then access that when saving.
What exactly is shown in the output? And from what I’m seeing right now,
You’re not actually setting the Particle.Enabled/Sound.Volume property after the data is loaded in when the player joins. You’re just setting values in the “DataToSave” table.
Instead of the code snippet that I quoted above, try setting the property of the particles/sound to the saved datastore value if that makes sense.
Since you’re saving your datastore in a table that looks like this:
{
ParticleData,
SoundData;
}
Data[1] would represent your ParticleData and Data[2] would represent SoundData. And just like in the PlayerAdded event where you find each Particle/Sound and set it’s desired value, you would do the same after you fetched the player’s data.
You could copy/paste the for loops that you did in the beginning of your PlayerAdded event with one change and add it inside of the if Success then statement :
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
Particle.Enabled = Data[1] --Setting the ENABLED property to the fetched player data.
end
end
for i, Sound in pairs(game.SoundService:GetDescendants()) do
if Sound.Name ~= "BackgroundMusic" and Sound:IsA("Sound") then
Sound.Volume = Data[2] --Setting the VOLUME property to the fetched data
end
end
if Success then
for i, Particle in pairs(game.Workspace:GetDescendants()) do
if Particle:IsA("ParticleEmitter") then
Particle.Enabled = Data[1]
end
end
for i, Sound in pairs(game.SoundService:GetDescendants()) do
if Sound.Name ~= "BackgroundMusic" and Sound:IsA("Sound") then
Sound.Volume = Data[2]
end
end
else
warn(Error)
end