Weird Issue I'm having with Module Script

I’ve been working on a game recently, and I’m setting up a way to save data, and part of that is storing all of the data while the players are in-game.

My issue is that when I try to create new data in the module script from a server script, it is not working.

My script:

local Players = game:GetService("Players")
local PlayerDataModule = require(script.Parent.PlayerDataModule)

local DefaultData = {
    ["Level"] = 1,
    ["Credits"] = 0
}
function SaveData(player)
    PlayerDataModule[tostring(player.UserId)] = DefaultData
    print(PlayerDataModule) -- only prints the things I already had there
end

Players.PlayerAdded:Connect(function(plr)
    SaveData(plr)
end)

When it prints the contents of the module script, nothing that was supposed to be added is there. This is probably some stupid mistake

What are the contents of your module? We can’t help you until we see all of the code involved in the problem.

By the way,
When connecting a function to an event, a more efficient and cleaner way of doing it is like this:

Players.PlayerAdded:Connect(SaveData)

The contents of my module are just test things so far. Inside the module is really just

local PlayerData = {}

PlayerData.TestData = "Hello"
PlayerData.Data = 1234

return PlayerData

Also, I am connecting more than one function to the PlayerAdded event later, so I can’t use that.

When I print the contents of the module script, it only prints “Hello” and 1234.

What else is it supposed to print?

The data that I added in under the SaveData function.

Try removing PlayerData.TestData and PlayerData.Data.

Run the script again, and see what prints. If nothing prints, try changing the original print function to print(PlayerDataModule[tostring(player.UserId)]

When I test it in my own workspace it is working just fine, with the output being:

▼{
 ["205615170"] =  ▼  {
                       ["Credits"] = 0,
                       ["Level"] = 1
                    },
                    ["Data"] = 1234,
                    ["TestData"] = "Hello"
                 }
1 Like

The only thing I changed is the module script’s location, but if there are no errors in your script that shouldn’t be the issue.

I’m running everything in ServerScriptService, if that has any relevance.

Ok I found the issue. I somehow did not see that I was missing an argument in one of my functions. Sorry for overlooking that.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.