DataStore won't work when i set BoolValue to true

Hello! i’m doing DataSaving system for my game. DataSaving system saving Player Settings. And theres setting for mute/unmute songs in-game. When i set BoolValue to true, Data Saving won’t work.

Script:

local DataStore = game:GetService("DataStoreService")
local SettingsData = DataStore:GetDataStore("PlayerSettingsData")

game.Players.PlayerAdded:Connect(function(Player)
local PlayerSettings = Instance.new("Folder")
	PlayerSettings.Parent = Player
	PlayerSettings.Name = "PlayerSettings"

	local Musics = Instance.new("BoolValue")
	Musics.Name = "Musics"
	Musics.Value = SettingsData:GetAsync(Player.UserId) or true
	Musics.Parent = PlayerSettings

SettingsData:SetAsync(Player.UserId, PlayerSettings.Musics.Value)

game.Players.PlayerRemoving:connect(function()
		SettingsData:SetAsync(Player.UserId, PlayerSettings.Musics.Value)
	end)
end)
1 Like

Add this at the bottom:

game:BindToClose(function()
    for _, player in Players:GetPlayers() do
        SettingsData:SetAsync(player.UserId, PlayerSettings.Musics.Value)
    end
end)

I tried but sorry, it didn’t work :frowning:

Mb

game.Players.PlayerRemoving:connect(function()
		SettingsData:SetAsync(Player.UserId, player.PlayerSettings.Musics.Value)
	end)
end)

You should not be using PlayerRemoving inside of the PlayerAdded handler. PlayerRemoving will fire for any player that leaves the game.

That being said, your initial logic to load the value will fail: SettingsData:GetAsync(Player.UserId) or true. That will always result in true because how the conditional statement works. Check explicitly for the nil value:

local loaded = SettingsData:GetAsync(player.UserId)
Musics.Value = if loaded ~= nil then loaded else true

Thank you so much! it works now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.