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
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
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
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.
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.
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?