Module nil on local script

The issue I’m having right now is every time I put the module on a local script and try to print anything from it, it comes up as nil.

Ex: I try to print Module.PlayerDataStore[UserID] and it comes up as nil.

I’m not sure why it’s coming up as nil. I put the module in replicated storage, so it should be replicated to the client, right?

Script:

-- Variables --
local Module = require(game.ReplicatedStorage.PlayerData)

local Player = game.Players.LocalPlayer

local UserID = Player.UserId

local Char = Player.Character or Player.CharacterAdded

local StaminaBar = script.Parent

-- Settings --

local TweenSpeed = 1

-- Script --

local function UpdateBar()
    local Percentage = 
        (Module.PlayerDataStore[UserID].Stamina/Module.PlayerDataStore[UserID].MaxStamina)

    StaminaBar:TweenSize(UDim2.new(Percentage,0,1,0),Enum.EasingDirection.Out,Enum.EasingStyle.Quad,TweenSpeed)
    print(Module.PlayerDataStore[UserID].Stamina.. " | Stamina!")
end

while Module.PlayerDataStore[UserID].Stamina < Module.PlayerDataStore[UserID].MaxStamina do
    UpdateBar()
end
1 Like

May I see the module script please?

1 Like

Sure

local PDModule = {}

-- This will hold all of the player's Data for future use.
PDModule.PlayerDataStore = {}

---------------------------------------------------------------------------------------------------------------------------------------------------------
-- Imports Data from joining players into the PlayerDataStore.
function PDModule.Accountcreation(Player)
	-- Script Variables --
	local PlayerID = Player.UserId
	print (PlayerID)

	-- Setting Up Player Data
	PDModule.PlayerDataStore[PlayerID] = {PlayerName = game.Players:GetNameFromUserIdAsync(PlayerID), Speed = 0, Strength = 0, Endurance = 100, CombatStatus = "None", Stamina = 100, MaxStamina = 100, Combat = false, Countdown = 30, CanRegen = true, CanStaminaRegen = true}

	print (PDModule.PlayerDataStore)
end
---------------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------------
-- Updates a Specific stat depending on both of the variables inserted into the parameters.
function PDModule.UpdateStat(Useless,Player,Stat,Value)


	-- Script Variables --
	local Functions = require(game.ServerStorage.Functions)
	local PlayerID = Player.UserId


	    -- Checking to see if the Value is a "String" or a Number, and acting accordingly.

		if typeof(Value) == "string" then

			-- Updating Player Data (For String)
		PDModule.PlayerDataStore[PlayerID][Stat] = Value

			
			
		elseif typeof(Value) == "number" then
		
			-- Updating Player Data (For Number)
		PDModule.PlayerDataStore[PlayerID][Stat] = PDModule.PlayerDataStore[PlayerID][Stat]+Value

			
		end

	print (Stat.." has been updated to. . . "..PDModule.PlayerDataStore[PlayerID][Stat])

    -- Updating the ACTUAL CHARACTER / Effects on the Player.
	print ("Updating Char.")
	Functions:UpdateCharacter(Player)
	print ("Updated Character . . !")
end
---------------------------------------------------------------------------------------------------------------------------------------------------------


return PDModule

Do you mean this whole module script code is in the local script or a module script?

1 Like

Firstly I would check whether the players data is actually in the table.

1 Like

I just wanted to see the module which you were requiring.

edit: Forget what I said I misread.

It’s in a module script but recognized by the local script

I already showed you the module above but, the data is in the module, I checked before in other scripts by printing. It’s just that the local script isn’t recognizing it for some reason

Try use :WaitForChild() on requiring the module script

you mean like

local Module = require(game.ReplicatedStorage:WaitForChild(“PlayerData”)) ?

1 Like

Yeah, also make sure you actually have the data value. Do you have recursion function which updates new data? Since you might not even have the Stamina Variable.

Edit: Unless the :WaitForChild() worked.

It’s saying that I’m attempting to index nil with stamina still, the waitforchild didn’t work

Edit: The stamina variable is inside the module

Can you print the data and send it here so we can see please?


▼  {
                    [13211894] =  ▼  {
                       ["CanRegen"] = true,
                       ["CanStaminaRegen"] = true,
                       ["Combat"] = false,
                       ["CombatStatus"] = "None",
                       ["Countdown"] = 30,
                       ["Endurance"] = 100,
                       ["MaxStamina"] = 100,
                       ["PlayerName"] = "albusseverus",
                       ["Speed"] = 0,
                       ["Stamina"] = 100,
                       ["Strength"] = 0
1 Like

Oh I don’t see any issue’s here, maybe there’s a typo. Have you tried changing it to Module.PlayerDataStore[UserID]["Stamina"] instead?

Edit: I would recommend you try something else as well. Create a function in the module:

-- Module Script
function PDModule:GetData(player)
   return self.PlayerDataStore
end

--Client Script
local PlayerData = module:GetData(player)

-- Now you should be able to do
local Stamina = PlayerData.Stamina

This is the only thing I could think of right now. I will be going offline so I won’t be able to reply but when I’m back on and there still isn’t a solution I will try and help. Thank you.

2 Likes

I tried that solution and PlayerData.Stamina still comes up as nil, I’m not sure what’s going on.

Modification to modules’ contents doesn’t happen across the client-server boundary, you’d have to use remote events.

1 Like

I don’t exactly get what you mean, are you saying that I’d have to make a remote event to fire the value of stamina to the local script every time it changes rather than passing it through the module?

1 Like