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

How to fix DataStore Tables Value Error (Valid Member) ?

Asked by 3 years ago

So first I had a error and somebody helped me with it but now it has a second.I'm new to this with tables and dont have a clue what to fix there i tried wait for child and FindFirst Child but both didnt work.(Maybe i did it wrong)

Error: PumpkinCave is not a valid member of Folder "Players.RedXgamerfausn.HalloweenGates"

Script:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("HalloweenGatesStore") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
    player:WaitForChild("HalloweenGates")

    local tableToSave = {
        player.HalloweenGates.PumpkinCave.Value; -- First value from the table
        player.HalloweenGates.GiantPumpkin.Value; -- Second value from the table
        player.HalloweenGates.HauntedEntrance.Value; -- Third
        player.HalloweenGates.HauntedBiome.Value --Fourthed
    }

    local success, err = pcall(function()
        dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
    end)

    if success then -- If the data has been saved
        print("Data has been saved!")
    else -- Else if the save failed
        print("Data hasn't been saved!")
        warn(err)       
    end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game

    -- // Assigning player stats //
        local HalloweenGates = Instance.new("Folder")
    HalloweenGates.Name = "HalloweenGates"
    HalloweenGates.Parent = player

    local PumpkinCave = Instance.new("BoolValue")
    PumpkinCave.Name = "Pumpkincave"
    PumpkinCave.Parent = HalloweenGates

    local GiantPumpkin = Instance.new("BoolValue")
    GiantPumpkin.Name = "GiantPumpkin"
    GiantPumpkin.Parent = HalloweenGates

    local HauntedEntrance = Instance.new("BoolValue")
    HauntedEntrance.Name = "HauntedEntrance"
    HauntedEntrance.Parent = HalloweenGates

    local HauntedBiome = Instance.new("BoolValue")
    HauntedBiome.Name = "HauntedBiome"
    HauntedBiome.Parent = HalloweenGates

    local data -- We will define the data here so we can use it later, this data is the table we saved
    local success, err = pcall(function()

        -- Get the data from the datastore or assign a default table if there is none
        data = dataStore:GetAsync(player.UserId) or { false, false, false, false }

    end)

    if success then -- If there were no errors and player loaded the data

        PumpkinCave.Value = data[1] -- Set the Value to the first value of the table (data)
        GiantPumpkin.Value = data[2] -- Set the Value to the second value of the table (data)
        HauntedEntrance.Value = data[3]--  ^^
        HauntedBiome.Value = data[4]--     ^^

    else -- The player didn't load in the data, and probably is a new player
        print("The player has no data!") -- The default will be set to 0
    end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
    local success, err  = pcall(function()
        saveData(player) -- Save the data
    end)

    if success then
        print("Data has been saved")
    else
        print("Data has not been saved!")
        warn(err)
    end
end)

game:BindToClose(function() -- When the server shuts down
    for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
        local success, err  = pcall(function()
            saveData(player) -- Save the data
        end)

        if success then
            print("Data has been saved")
        else
            print("Data has not been saved!")
            warn(err)
        end
    end
end)

Answer this question