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

WHY does this seemingly illogical error happen?

Asked by
dispeller 200 Moderation Voter
6 years ago

So I have this module, and whenever I try to call this function:

(around line 22)

-- Read data function
 function PlayerStatManager:GetData(player, statName)
    warn("DATA : ",unpack(sessionData))
    return sessionData[player.Name][statName]
end

it gives me this error:

ServerScriptService.SaveDataModule:22: attempt to concatenate a nil value

This is the entire module. It is a modified version of the one on the official Roblox wiki page

-- Setup table that we will return to scripts that require the ModuleScript.
PlayerStatManager = {}

-- Create variable for the DataStore.
DataStoreService = game:GetService('DataStoreService')
playerData = DataStoreService:GetDataStore('PlayerData2'.. game.PlaceId )

-- Create variable to configure how often the game autosaves the player data.
AUTOSAVE_INTERVAL = 60

-- Table to hold all of the player information for the current session.
sessionData = {}

-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
    sessionData[player.Name][statName] = sessionData[player.Name][statName] + changeValue
end

-- Read data function
 function PlayerStatManager:GetData(player, statName)
    warn("DATA : ",unpack(sessionData))
    return sessionData[player.Name][statName]
end

-- Function to retrieve player's data from the DataStore.
local function getPlayerData(player)
    return playerData:GetAsync(player.UserId)
end

-- Function to save player's data to the DataStore.
local function savePlayerData(player)
    playerData:SetAsync(player.UserId, sessionData[player.Name])
end

-- Function to add player to the sessionData table. First check if the player has
-- data in the DataStore. If so, we'll use that. If not, we'll add the player to
-- the DataStore.
local function setupPlayerData(player)
    local data = getPlayerData(player)
    if not data then
        -- DataStores are working, but no data for this player
        sessionData[player.Name] = {Money = 0,
                               MaxHealth = 100,
                               MaxShield = 100,
                               ["tools"] = {
                                 -- {"Sword","Classic sword",{}},
                                 {"Ax","Classic ax",{"Speedy","Strong"},{2,3}}
                               },
                               ["items"] = {
                                 {"Wood",5,"The stuff of trees"},
                                 {"Fish",3,"a limbless animal with gills and fins"}
                               }

                              }

        savePlayerData(player)
    else
        -- DataStores are working and we got data for this player
        sessionData[player.Name] = data
    end
end

-- Function to run in the background to periodically save player's data.
local function autosave()
    while wait(AUTOSAVE_INTERVAL) do
        for player, data in pairs(sessionData) do
            savePlayerData(player)
        end
    end
end

-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)

-- Call savePlayerData on PlayerRemoving to save player data when they leave.
-- Also delete the player from the sessionData, as the player isn't in-game anymore.
game.Players.PlayerRemoving:connect(function(player)
    savePlayerData(player)
    sessionData[player.Name] = nil
end)

-- Start running autosave function in the background.
spawn(autosave)

-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager


-- PlayerStatManager:ChangeStat(player, statName, changeValue)
-- PlayerStatManager:GetData(player, statName)
-- https://www.roblox.com/Thumbs/Asset.ashx?width=110&height=110&assetId=1818

what is annoying me is the fact that the script errors because it thinks sessionData is undefined when I try to call it in

warn("DATA : ",unpack(sessionData))

and sessionData is clearly defined a few lines above

0
on line 22, try warn("DATA: "..unpack(sessionData)) i have no idea what the problem is but from what I know, that is what the error is likely saying greatneil80 2647 — 6y
0
I've tried that and it didn't work. That was what I originally put, but a friend told me to use , instead of .. because it is better form dispeller 200 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Concatenating is where the script places a variable inside of text. Read more here

First off, try printing just the sessionData table. See if it has any data. If not, check your data adding code.

The correct form of concatenating would be:

warn("DATA : "..unpack(sessionData))

Viewing the unpack script would be very handy in this case, for that could be malfunctioning also.

0
I've tried that and it didn't work. That was what I originally put, but a friend told me to use , instead of .. because it is better form. Also, unpack is a built in Roblox Lua function. I'm not a noob. dispeller 200 — 6y
Ad

Answer this question