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

Error when attempting to call function from module?

Asked by 5 years ago

Using this ModuleScript from the wiki:

game:GetService("Players").PlayerAdded:Connect(function(player)
    local stats = Instance.new("IntValue")
    stats.Name = 'leaderstats'
    stats.Parent = player

    local money = Instance.new("NumberValue")
    money.Name = 'Money'
    money.Parent = stats

    local experience = Instance.new("NumberValue")
    experience.Name = 'Experience'
    experience.Parent = stats
end)


-- Set up table to return to any script that requires this module script
local PlayerStatManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("KardashevData")

-- Table to hold player information for the current session
local sessionData = {}

local AUTOSAVE_INTERVAL = 60

-- Function that other scripts can call to change a player's stats
function PlayerStatManager:ChangeStat(player, statName, value)
    local playerUserId = "Player_" .. player.UserId
    assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
    if typeof(sessionData[playerUserId][statName]) == "number" then
        sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
    else
        sessionData[playerUserId][statName] = value
    end
end

-- Function to add player to the 'sessionData' table
local function setupPlayerData(player)
    local playerUserId = "Player_" .. player.UserId
    local data
    local success, err = pcall(function()
        playerData:UpdateAsync(playerUserId, function(playerData)
            data = playerData
        end)
    end)

    if success then
        if data then
            -- Data exists for this player
            sessionData[playerUserId] = data
        else
            -- Data store is working, but no current data for this player
            sessionData[playerUserId] = {Money=15, Experience=0}
        end
    else
        warn("Cannot set up data for player!")
    end
end

-- Function to save player's data
local function savePlayerData(playerUserId)
    if sessionData[playerUserId] then
        local success, err = pcall(function()
            playerData:SetAsync(playerUserId, sessionData[playerUserId])
        end)
        if not success then
            warn("Cannot save data for player!")
        end
    end
end

-- Function to save player data on exit
local function saveOnExit(player)
    local playerUserId = "Player_" .. player.UserId
    savePlayerData(playerUserId)
end

-- Function to periodically save player data
local function autoSave()
    while wait(AUTOSAVE_INTERVAL) do
        for playerUserId, data in pairs(sessionData) do
            savePlayerData(playerUserId)
        end
    end
end

-- Start running 'autoSave()' function in the background
spawn(autoSave)

-- Connect 'setupPlayerData()' function to 'PlayerAdded' event
game.Players.PlayerAdded:Connect(setupPlayerData)

-- Connect 'saveOnExit()' function to 'PlayerRemoving' event
game.Players.PlayerRemoving:Connect(saveOnExit)

return PlayerStatManager

When I try to call the :ChangeStat() function,

local module = require(game:GetService("ServerStorage"):WaitForChild("ModuleScript"))

game:GetService("Players").PlayerAdded:Connect(function(player)
    module:ChangeStat(player, 'Money', 5)
end)

I get this error: ServerStorage.ModuleScript:30: attempt to index field '?' (a nil value)

Also, it seems that this line doesn't run at all: sessionData[playerUserId] = {Money=15, Experience=0} as new players start with 0, instead of 15. Any help would be appreciated, thanks.

0
Just delete line 30. Its useless NiniBlackJackQc 1562 — 5y

Answer this question