Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why is BoolValue.Changed not working?

Asked by 3 years ago

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)
0
Also, PlayerRemoving also doesn't let me save the BoolValue, please can someone tell me why MelodicSea5914 50 — 3y
0
You're adding .Changed after changing the value, it can never catch the change if its already happened. DinozCreates 1070 — 3y
0
So what should I do? MelodicSea5914 50 — 3y
0
Move the .Changed above where the change happens. DinozCreates 1070 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

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)
0
Oh Ok. I will change it to an IntValue an see if it works. I will get to you if it works MelodicSea5914 50 — 3y
Ad

Answer this question