Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Boolvalue saving datastore loads all boolvalues as true no matter what?

Asked by 3 days ago

I'm trying to make a boolvalue saving datastore to save cars. Each car has it's own boolvalue (once you buy a car it's boolvalue is set to true), and when it goes to save the values it only saves the name of the boolvalue that is true. So when it loads in the datastore, it checks for that name and since it's already true it makes that boolvalue true. However, my script doesn't work. No matter what, even if all of the boolvalues are false, they are all set to true once they are loaded. Here's the script:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("CarData1")
local scanfolder = game.ReplicatedStorage.CarValues:GetChildren()


game.Players.PlayerAdded:Connect(function(player)
    local success, errormessage = pcall(function()
    local saved = ds:GetAsync(player.UserId.."-cardata") or {}
        if saved then
            for i, v in pairs(saved) do
                local addvalue = game.ReplicatedStorage.CarValues:FindFirstChild(v)
                addvalue.Value = true
            end
        end 
    end)    
    if errormessage then warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local valuetable = {}
    for i, v in pairs(scanfolder) do
        if v.Value == true then
        table.insert(valuetable, v.Name)
        end
    end
    local success, errormessage = pcall(function()
        ds:SetAsync(player.UserId.."-cardata", valuetable)
    end)
    if errormessage then warn(errormessage)
    end
end)

game:BindToClose(function()
    for i, v in pairs(game.Players:GetPlayers()) do
        local player = v

        local valuetable = {}
        for i, v2 in pairs(scanfolder) do
            if v2.Value == true then
            table.insert(valuetable, v2.Name)
            end 
        end
        local success, errormessage = pcall(function()
            ds:SetAsync(player.UserId.."-cardata", valuetable)
        end)
        if errormessage then
            warn(errormessage)
        end
    end
end)

Any help would be appreciated!

0
I think the problem is in line 12? You attempted to set loaded data value to true Feelings_La 21 — 2d

Answer this question