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

attempt to index local 'player' a nil value?

Asked by 4 years ago
Edited 4 years ago
_G.Data = {
koins = 500,
level = 1,
rank = "Bronze",
print('make sure to add jersey'),
--Helmet = (link)
--Helmet.Name = "Helmet",
--local mouthp = Instance.new("MeshPart",Data),
--mouthp.Name = "Mouthpiece",
xp = 1
}

local DSS = game:GetService("DataStoreService")

local datastore = DSS:GetDataStore("YourChampion")


function generateDataKey(player)
        local ret = "cid_"..player.userId
        return ret
end


for index,value in pairs (_G.Data) do
    datastore:UpdateAsync(index, function() return value end) -- index will be our 
end 



function generateDataTable(player)
    local dataTable = {
        koins = player.PlayerGui.Nice.Data.koins.Value,
        level = player.PlayerGui.Nice.Data.Level.Value,
        xp = player.PlayerGui.Nice.Data.xp.Value,
        rank = player.PlayerGui.Nice.Data.Rank.Value
    }
    return dataTable
end

function saveDataForPlayer(player)
local Success, ErrorMsg = pcall(function()
  print('Saving Data') 
  local key = generateDataKey(player)
  local data = generateDataTable(player) 
  datastore:SetAsync(key, data) 
end)

if not Success then
warn(ErrorMsg) -- if it errors, it will provide a reason on why it errored 
end
    local key = generateDataKey(player)
    local data = generateDataTable(player)
    datastore:SetAsync(key, data)
end

function inputDataToPlayer(player, data)
    player.PlayerGui.Nice.Data.koins.Value = data.koins
    player.PlayerGui.Nice.Data.Level.Value = data.Level
    player.PlayerGui.Nice.Data.xp.Value = data.xp
    player.PlayerGui.Nice.Data.Rank.Value = data.Rank
end

function loadDataForPlayer(player)
    local key = generateDataKey(player)
    local data = datastore:GetAsync(key)
    inputDataToPlayer(player, data)
end

if _G.Data == nil then
    inputDataToPlayer()
else
    loadDataForPlayer()
end


game.Players.PlayerAdded:connect(function(player)
    loadDataForPlayer(player) --Load first thing when they join
                player.PlayerGui.Nice.Data.Rank.Changed:connect(function()
                    saveDataForPlayer(player)
                    end)
end)

game.Players.PlayerRemoving:connect(saveDataForPlayer()) --Save data when the player leaves the game
`Workspace.Data:19: attempt to index local 'player' (a nil value)`
0
Please post the code, where you used the generateDataKey function cegberry 432 — 4y
0
edited smash_abIe 19 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Firstly, use :Connect not :connect. The latter is deprecated and may stop working at any point.

Secondly, as the error clearly states, you are attempting to index something for your variable 'player' which hasn't been defined. This can be troubleshooted by going through your code and looking at all the places you use your 'player' variable, then see if the player has been defined at that point.

Going through your code, the 'player' variable is indeed NOT defined for your functions because they all take in the 'player' parameter but that variable is not passed when you actually call the functions here:

if _G.Data == nil then
    inputDataToPlayer()
else
    loadDataForPlayer()
end

and here:

game.Players.PlayerRemoving:connect(saveDataForPlayer())

To fix this, simply put the 'player' variable in the parenthesis so that you pass that variable to your functions.

Ad

Answer this question