ModuleScript has trouble with getting player values

Hello, I’m trying to use a ModuleScript to get values inside a leaderstats folder in the player, so I can use them as a variable globally. This specific part of the module does not work, breaking the entire thing and all of the scripts that use it. Heres the part where it doesnt work and the script calling the init()

--the part of the module where it broke
function module.init(plr, valUpgStat, timeUpgStat, capUpgStat)
	print(valUpgStat)
	local valUpg = valUpgStat.Value
	local timeUpg = timeUpgStat.Value
	local capUpg = capUpgStat.Value
	local plr = plr

	return {
		valUpg = valUpg,
		timeUpg = timeUpg,
		capUpg = capUpg,
		plr = plr
	}
end

local data = module.init()
print(data.valUpg)
--general localscript
print("Running")
local SGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")
local module = require(RepStorage:WaitForChild("ModuleScript"))
SGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
SGui:SetCoreGuiEnabled(Enum.CoreGuiType.EmotesMenu,false)
SGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)
local plr = Players.LocalPlayer
local char = plr.Character
local leaderstats = plr:WaitForChild("leaderstats")
local valUpgStat = leaderstats:WaitForChild("valupg")
local timeUpgStat = leaderstats:WaitForChild("timeupg")
local capUpgStat = leaderstats:WaitForChild("capupg")

repeat wait() until char

print("Found character")
local humanoid = char:FindFirstChildOfClass("Humanoid")
if humanoid then
	print("Found humanoid")
	humanoid.WalkSpeed = 25
end
print(valUpgStat)
local moduleVars = module.init(plr, valUpgStat, timeUpgStat, capUpgStat) 
print(moduleVars.plr)
print(moduleVars.valUpg)
print(moduleVars.timeUpg)
print(moduleVars.capUpg)

Appreciate any help!

It seems that the init function inside the module script is expecting some arguments, but when you call it in the local script, you’re not passing any arguments.

In the local script, you need to pass the arguments plr , valUpgStat , timeUpgStat , and capUpgStat when you call module.init() .
Change this line:

local data = module.init()

to:

local data = module.init(plr, valUpgStat, timeUpgStat, capUpgStat)

This should be the fix. If it isnt just come back to me and ill see if i can work it out lol

Okay this is bad on my part
Forgot to include the error and the line where it doesn’t work
Error

ReplicatedStorage.ModuleScript:21: attempt to index nil with 'Value'

Script

function module.init(plr, valUpgStat, timeUpgStat, capUpgStat)
	print(valUpgStat)
	local valUpg = valUpgStat.Value --its this line with the error
	local timeUpg = timeUpgStat.Value
	local capUpg = capUpgStat.Value
	local plr = plr

	return {
		valUpg = valUpg,
		timeUpg = timeUpg,
		capUpg = capUpg,
		plr = plr
	}
end

Also I tried your solution but the variables are an unknown global so it dont work
@fvtted

@fvtted aaaa any response? it’s been a bit

Can you show me where the function gets called, what you pass through the function when it is called, and the stat itself?

The error means that one or some of the parameters valUpgStat, timeUpgStat and capUpgStat aren’t passed to the function or the instances you pass are nil. As @fvtted mentioned you call init without parameters which will cause the issue I just mentioned, and thus to fix it you must pass the parameters to init when calling the function(when a parameter isn’t passed to a function, the function assumes the said parameter is nil).

but… it doesn’t work though i’ve stated that already

Issue has been fixed. I just removed this part of the code entirely

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