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

"invalid argument to pairs (table expected got nil)" but when I fix it the GUI stops working?

Asked by 2 years ago

Ok so I have a serverScriptService script called "Data" and its meant to load your data right? heres the first version of it Version 1

local DataStore = game:GetService("DataStoreService"):GetDataStore("HahaisdeadSave39")
local replicatedStorage = game:GetService("ReplicatedStorage")
local saveRemote = replicatedStorage:WaitForChild("Save")



--creating data

game.Players.PlayerAdded:Connect(function(player)

    wait()

    local SpinSystem = player.PlayerGui:WaitForChild("SpinSystem")

    local folder = Instance.new("Folder", player)
    folder.Name = "Data"


    local hasgame = Instance.new("BoolValue", folder)
    hasgame.Name = "HasGame"

    local hair = Instance.new("StringValue", folder)
    hair.Name = "Hair"

    local outfit = Instance.new("StringValue", folder)
    outfit.Name = "Outfit"

    local accessory = Instance.new("StringValue", folder)
    accessory.Name = "Accessory"

    local Gender = Instance.new("StringValue", folder)
    Gender.Name = "Gender"

    local blood = Instance.new("StringValue", folder)
    blood.Name = "Blood"

    local spins = Instance.new("IntValue", folder)
    spins.Name = "Spins"



    --visibiliy of spin button variable




    ------------------------------------------------------------------------------------------------------------------------SAME HERE


    local success, errormsg = pcall(function()
        local getData = DataStore:GetAsync(player.UserId)
        for i, v in pairs(getData) do
            if i == 1 then
                hasgame.Value = getData[i]
            elseif i == 2 then 
                hair.Value = getData[i]
            elseif i == 3 then
                outfit.Value = getData[i]
            elseif i == 4 then
                accessory.Value = getData[i]
            elseif i == 5 then
                blood.Value = getData[i]

            elseif i == 6 then
                spins.Value = getData[i]

            end
        end
    end)

    ----------------------------------------------OR

    --local success, errormsg = pcall(function()
        --return DataStore:GetAsync(player.UserId)
    --end)

    ----------------------------------------------------------------------------------------------

    if success then
        print("Successfully loaded "..player.Name.."'s data!")
    elseif not success then
        -- FIRST TIME JOINING --
        hasgame.Value = false
        print("hasgame has been changed to false")
        hair.Value = "Men Hair"
        print("hair value has been changed to men hair")
        outfit.Value = "Luffy's outfit"
        print("outfit has been changed to luffys outfit")
        accessory.Value = "Shades"
        print("accessory has been changed to shades")
        Gender.Value = "Male"
        print("gender has been changed to none")
        blood.Value = "Burgundy"
        print("blood has been set to burgundy")
        spins.Value = 8 



        error("Something went wrong while loading the data of "..player.Name..": "..errormsg)
    end

end)

saveRemote.OnServerEvent:Connect(function(player, val)
    DataStore:SetAsync(player.UserId, val)
    print("Successfully saved "..player.Name.."'s data!")
end)

Now this makes the GUI work perfectly fine except it doesnt load the data properly and therefore the data doesnt stay

my friend suggested that this part here was bad

local success, errormsg = pcall(function()
        local getData = DataStore:GetAsync(player.UserId)
        for i, v in pairs(getData) do
            if i == 1 then
                hasgame.Value = getData[i]
            elseif i == 2 then 
                hair.Value = getData[i]
            elseif i == 3 then
                outfit.Value = getData[i]
            elseif i == 4 then
                accessory.Value = getData[i]
            elseif i == 5 then
                blood.Value = getData[i]

            elseif i == 6 then
                spins.Value = getData[i]

            end
        end
    end)

so instead i was given this which did solve the problem and got rid of the error

local DataStore = game:GetService("DataStoreService"):GetDataStore("HahaisdeadSave39")
local replicatedStorage = game:GetService("ReplicatedStorage")
local saveRemote = replicatedStorage:WaitForChild("Save")



--creating data

game.Players.PlayerAdded:Connect(function(player)

    wait()

    local SpinSystem = player.PlayerGui:WaitForChild("SpinSystem")

    local folder = Instance.new("Folder", player)
    folder.Name = "Data"


    local hasgame = Instance.new("BoolValue", folder)
    hasgame.Name = "HasGame"

    local hair = Instance.new("StringValue", folder)
    hair.Name = "Hair"

    local outfit = Instance.new("StringValue", folder)
    outfit.Name = "Outfit"

    local accessory = Instance.new("StringValue", folder)
    accessory.Name = "Accessory"

    local Gender = Instance.new("StringValue", folder)
    Gender.Name = "Gender"

    local blood = Instance.new("StringValue", folder)
    blood.Name = "Blood"

    local spins = Instance.new("IntValue", folder)
    spins.Name = "Spins"



    --visibiliy of spin button variable




    ------------------------------------------------------------------------------------------------------------------------SAME HERE




    ----------------------------------------------OR

    local success, errormsg = pcall(function()
        --return DataStore:GetAsync(player.UserId)
    end)

    ----------------------------------------------------------------------------------------------

    if success then
        print("Successfully loaded "..player.Name.."'s data!")
    elseif not success then
        -- FIRST TIME JOINING --
        hasgame.Value = false
        print("hasgame has been changed to false")
        hair.Value = "Men Hair"
        print("hair value has been changed to men hair")
        outfit.Value = "Luffy's outfit"
        print("outfit has been changed to luffys outfit")
        accessory.Value = "Shades"
        print("accessory has been changed to shades")
        Gender.Value = "Male"
        print("gender has been changed to none")
        blood.Value = "Burgundy"
        print("blood has been set to burgundy")
        spins.Value = 8 



        error("Something went wrong while loading the data of "..player.Name..": "..errormsg)
    end

end)

saveRemote.OnServerEvent:Connect(function(player, val)
    DataStore:SetAsync(player.UserId, val)
    print("Successfully saved "..player.Name.."'s data!")
end)

its this part right here that replaces the other part

local success, errormsg = pcall(function()
        --return DataStore:GetAsync(player.UserId)
    end)

This is version 2 now yeah it gets rid of the error but it doesnt make loading the data possible and since my GUI relies on data it doesnt work properly

I really dont know what other alternatives I can do

I wanna stick with version 1 because it actually makes the GUI work and load up the save

Answer this question