Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is BoolValue.Changed not working?

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)

4 comments

0

Also, PlayerRemoving also doesn't let me save the BoolValue, please can someone tell me why

0

You're adding .Changed after changing the value, it can never catch the change if its already happened.

0

So what should I do?

0

Move the .Changed above where the change happens.

1 answer

✓ Accepted answer

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)

1 comment

0

Oh Ok. I will change it to an IntValue an see if it works. I will get to you if it works

This discussion is preserved as it was. New answers, comments, and votes are closed.