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

Attempt to index nil with number on DataStore script?

Asked by 3 years ago

So I updated my dataStore script and its giving me this error "Attempt to index nil with number"

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



local function saveData(player) -- The functions that saves data

    local tableToSave = {
        player.Data.HasGame.Value, -- First value from the table
        player.Data.Hair.Value,
        player.Data.Outfit.Value,
        player.Data.Accessory.Value,
        player.Data.Gender.Value,
        player.Data.Spins.Value
    }


    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
        print("Data has been saved!")
    else
        print("Data hasn't been saved!")
        warn(err)       
    end
end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
    saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
    for _, player in pairs(game.Players:GetPlayers()) do
        saveData(player) -- Save the data
    end
end)

--creating data

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

    wait()

    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 data
    local success, err = pcall(function()

        data = DataStore:GetAsync(player.UserId)

    end)

    if success then

        HasGame.Value = data[1]
        Hair.Value = data[2]
        Outfit.Value = data[3]
        Accessory.Value = data[4]
        Gender.Value = data[5]
        Blood.Value = data[6]
        Spins.Value = data[7]

    else
        print("The player has no data!") -- The default will be set to 0
        HasGame.Value = false
        Hair.Value = "ConfirmedNewSave"
        Outfit.Value = "ConfirmedOutfitNewSave"
        Accessory.Value = "ConfirmedHornsNewSave"
        Gender.Value = "Male"
        Blood.Value = "Burgundy"
        Spins.Value = 8
    end

end)

and it points to line 94 the HasGame.Value = data[1] part

local data
    local success, err = pcall(function()

        data = DataStore:GetAsync(player.UserId)

    end)

    if success then

        HasGame.Value = data[1] --THIS PART HERE 
        Hair.Value = data[2]
        Outfit.Value = data[3]
        Accessory.Value = data[4]
        Gender.Value = data[5]
        Blood.Value = data[6]
        Spins.Value = data[7]

so therefore the bottom part where it selects the default values if the player is new doesnt work properly and therefore my Spinner gui doesnt work properly but thats besides the point

Why is it saying attempt to index nil with number on that part? the HasGame is a boolValue which is meant to detect if the player has been here before and doesnt exploit the SpinGui

Could it be that since the HasGame is a boolvalue and the rest are StringValues that it isnt working? or have I misplaced the numbers through data[1-7]?

0
maybe you could try setting the index on the tableToSave variable? For example, {[1] = HasGame.Value, [2] = Hair.Value} etc. the8bitdude11 358 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Correction,

I forgot to add Blood on the first part of tableToSave

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



local function saveData(player) -- The functions that saves data

    local tableToSave = {
        player.Data.HasGame.Value, -- First value from the table
        player.Data.Hair.Value,
        player.Data.Outfit.Value,
        player.Data.Accessory.Value,
        player.Data.Gender.Value,
        player.Data.Blood.Value,
        player.Data.Spins.Value
    }


    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
        print("Data has been saved!")
    else
        print("Data hasn't been saved!")
        warn(err)       
    end
end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
    saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
    for _, player in pairs(game.Players:GetPlayers()) do
        saveData(player) -- Save the data
    end
end)

--creating data

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

    wait()

    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 data
    local success, err = pcall(function()

        data = DataStore:GetAsync(player.UserId)

    end)

    if success then

        HasGame.Value = data[1]
        Hair.Value = data[2]
        Outfit.Value = data[3]
        Accessory.Value = data[4]
        Gender.Value = data[5]
        Blood.Value = data[6]
        Spins.Value = data[7]

    else
        print("The player has no data!") -- The default will be set to 0
        HasGame.Value = false
        Hair.Value = "ConfirmedNewSave"
        Outfit.Value = "ConfirmedOutfitNewSave"
        Accessory.Value = "ConfirmedHornsNewSave"
        Gender.Value = "Male"
        Blood.Value = "Burgundy"
        Spins.Value = 8
    end

end)

im still getting the error though, cant imagine why

Ad

Answer this question