I have two scripts. It is suppose to assign, create, and update data for each player. When running these scripts, it results in an error. How can I fix this error?

This is the ModuleScript (titled "ModuleScript") placed in ServerStorage:

local playerStatManager = {} --Saved data
local sessionData = {} --Current unsaved data

function playerStatManager:ChangeStat(player, statName, changeValue)
    sessionData[player][statName] = sessionData[player][statName] + changeValue
end

local function setupPlayerData(player)
    sessionData[player] = {Money = 0, Experience = 0}
end

game.Players.PlayerAdded:Connect(setupPlayerData)

local dataStoreService = game:GetService("DataStoreService")
local playerData = dataStoreService:GetDataStore("EvilData")

local function setupPlayerData(player)
    local data = playerData:GetAsync(player.UserID)
    if not data then
        data = {Money = 0, Experience = 0}
        playerData:SetAsync(player.UserID, sessionData[player])
    end
    sessionData[player] = data
end

local function savePlayerData(player)
    playerData:UpdateAsync(player.UserId, sessionData[player])
end

game.Players.PlayerRemoving:Connect(function(player)
    savePlayerData(player)
end)

return playerStatManager --What does this mean?

This is a regular script (titled "Script") placed in ServerScriptService:

local playerStatManager = require(game.ServerStorage.playerStatManager)

game.Players.PlayerAdded:Connect(function(player)
    while wait(2) do
        playerStatManager:ChangeStat(player, "Money", 5)
        playerStatManager:ChangeStat(player, "Experience", 1)
        print(playerStatManager)
    end
end)

The resulting error is as follows:

00:16:20.807 - playerStatManager is not a valid member of ServerStorage

00:16:20.808 - Stack Begin

00:16:20.808 - Script 'ServerScriptService.Script', Line 1

00:16:20.808 - Stack End

According to one of the solutions (provided by @Shawnyg), I made the attempt of replacing line 1 from the script under ServerScriptStorage.

Try using game.ServerStorage:WaitForChild('playerStatManager') if it continously yields, that means that module isn't there.

This resulted in a continuous yield. I don't understand what he/she means by "the module isn't there."

A screenshot of ServerScriptStorage: https://imgur.com/a/okCXudY

@RedcommanderV2 suggested that the error is as a result of having the name of the script titled "ModuleScript" and that I should change it. I changed the name to something different yet the error still remains.

Solution: The error was that the ModuleScript had to be named after the table "playerStatManager".

11 comments

0

if it's named 'ModuleScript', and you're searching for 'playerStatManager' there is no way it could work..

0

Also, it's not constructive because you didn't add a screenshot from how your serverstorage looks like, so there is very little chance that we can help you

1

@RedcommanderV2 why is that? Surprised how no tutorial goes over that as I've viewed about four of them.

1

@RedcommanderV2 They wanted a screenshot? They could've just asked me or explained to me instead of taking my post down with no explanation at all. Last time I checked this was a learning site.

0

:WaitForChild() takes a string and searches for an object with the name that's equal to what you gave, if something is named 'a' and you're searching for 'b' don't expect it to find it

0

if they can't understand a question, they'll most likely take it down as it's badly explained, we're here to help with specific questions, not to solve a script

1

I renamed ModuleScript but it didn't remedy the error. I'm frustrated.

0

can you update the screenshot?

1

Done.

1

You have to name it "playerStatManager" because that's how it's defined in your script

1

Ohh! I would have never known that.