Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

attempt to index local 'player' a nil value?

_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)`

2 comments

0

Please post the code, where you used the generateDataKey function

0

edited

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.