Log in to vote

Table keeps returning "nil" for some reason?

Asked by 1 year ago

so i am trying to use a table to store loadout data in my game, but every time i try to use the table it tells me it's just nil. i have 2 potential culprits in my loadout handling server-side script:

local dataStore = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverLoadouts = {}

game.Players.PlayerAdded:Connect(function(player) -- gets the player's loadout presets when they connect, gives them empty presets if there's no data
    local userId = player.UserId
    serverLoadouts[userId] = dataStore:GetDataStore("loadouts"):GetAsync(userId)
    if serverLoadouts[userId] == nil then -- checks if the player has loadout information
        serverLoadouts[userId] = {
--"empty" loadouts (just the loadout structuring but all the items are "empty", also has nested dictionaries)
        } -- ends the default loadout
        dataStore:GetDataStore("loadouts"):SetAsync(userId, serverLoadouts[userId]) -- gives the player "empty" loadouts
    end -- ends the if statement
end) -- ends the function PlayerAdded

which is supposed to take the loadout data from a datastore, and give a default loadout if they've never connected before


replicatedStorage.returnServerLoadouts.OnServerInvoke = function(clientId)
    return serverLoadouts[clientId]
end -- remote function for sending the server's loadout data to the client

which is the remote function for sending the server loadout data to the client. (client id is the client's userId)

any idea what's going wrong?

Answer this question