I’ve made so many posts about DataStores yet none have an answer. I don’t understand what’s going on, the script look just fine. For some reason when I rejoin the game after clicking the button (sets the playing value to false) it sets it to true again. Don’t know what’s going on.
I did the exact same format as I would with leaderstats (which works) but this doesn’t. It’s just a bool value instead of a number value.
local DataStoreService = game:GetService("DataStoreService")
local SettingsDataStore = DataStoreService:GetDataStore("SettingsDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Volume = game.SoundService:WaitForChild("BackgroundMusic")
Volume.Playing = SettingsDataStore:GetAsync(Player.UserId) or true
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Volume = game.SoundService:WaitForChild("BackgroundMusic")
SettingsDataStore:SetAsync(Player.UserId, Volume.Playing)
end)
If volume.Playing is false, it’s going to revert to true because of your use of the or operator. In your case, you have to explicitly check if it isn’t nil.
local playing = SettingsDataStore:GetAsync(Player.UserId)
if playing == nil then
playing = true
end
local DataStoreService = game:GetService("DataStoreService")
local SettingsDataStore = DataStoreService:GetDataStore("SettingsDataStore")
game.Players.PlayerAdded:Connect(function(Player)
local Playing = SettingsDataStore:GetAsync(Player.UserId)
if Playing == nil then
Playing = true
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Volume = game.SoundService:WaitForChild("BackgroundMusic")
SettingsDataStore:SetAsync(Player.UserId, Volume.Playing)
end)
Are you having more than 1-person servers? If so, the data is going to revert to whatever is playing on the server, regardless of what’s going on in the client.
Yeah, if the sound is paused on the server, it’s going to load as paused for every player that leaves while it is paused, same thing if it was playing.
So you should instead create a value inside of the player and adjust it to whatever the client specifies, probably via a remote event. Then on the client, play or pause it accordingly.
I think you should do what is mentioned above then. It could be simple a replication problem. You will have to send whether or not the sound is playing or not from the client and set the sound playing from the client too.
Then you can use the server to store the data.
Actually thinking about it it would be better to have the playerRemoving on the client
You are probably turning it off by client while trying to check if they turned it off by server. Meaning that it would have to be off on the server for it to save.
Edit: nvm @7z99 already said this
When the Player leaves Fire a Remote Event from the client contain the info on whether or not the sound is playing.
Then the server stores the info in the datastore.
game.Players.PlayerRemoving:Connect(function(Player)
local Volume = game.SoundService:WaitForChild("BackgroundMusic")
SettingsDataStore:SetAsync(Player.UserId, Volume.Playing)
print(Volume.Playing)
end)
The reason why this code probably does not work is because it is in the server while actual sound is on the client.put simply
To get around this use a RemoteEvent to send from the client to the server when the player leaves.
You also need to do this when the player joins send the information from the data store to the client so that the client can play is own sound