Hi! So I am making a system where you would buy a title in a shop (kind of like Epic Minigames) and when you leave it would save whether or not it has been bought. However, the brought Bool Value.Changed function does not work inside the onPlayerAdded function, however when I use the sae event outside the function, it activates. Please let me know why this is and how I should fix it please. Thank you.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("TitleSave1") local brought = script.Parent -- Get Bool Value local button = brought.Parent.BuyButton local function onPlayerAdded(player) brought.Value = ds:GetAsync(player.UserId) or false if brought.Value == true then button.Text = "Equip" end ds:SetAsync(player.UserId, brought.Value) brought.Changed:Connect(function() -- This does not activate print("Changed") ds:SetAsync(player.UserId, brought.Value) end) end game.Players.PlayerAdded:Connect(onPlayerAdded) game.Players.PlayerRemoving:Connect(function(player) ds:SetAsync(player.UserId, brought.Value) end)
Try To Save It As A Int Value Because You Can't Save BoolValues In Datastore Here Is A Script This Will Work With A BoolValue.
The Changed Event Only Fires On Server Not If The Change Is On The Client.
Updated Script:
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("TitleSave1") local brought = script.Parent -- Get Bool Value local button = brought.Parent.BuyButton local function onPlayerAdded(player) if ds:GetAsync(player.UserId) == true then brought.Value = true elseif ds:GetAsync(player.UserId) == false then brought.Value = false end if brought.Value == true then button.Text = "Equip" end if brought.Value == true then ds:SetAsync(player.UserId, 1) elseif brought.Value == false then ds:SetAsync(player.UserId, 0) end brought.Changed:Connect(function() -- This does not activate print("Changed") if brought.Value == true then ds:SetAsync(player.UserId, 1) elseif brought.Value == false then ds:SetAsync(player.UserId, 0) end end) end game.Players.PlayerAdded:Connect(onPlayerAdded) game.Players.PlayerRemoving:Connect(function(player) if brought.Value == true then ds:SetAsync(player.UserId, 1) elseif brought.Value == false then ds:SetAsync(player.UserId, 0) end end)