How could I match this value in a Module Script to a leaderstats value?

I want to make a lag test game in which I want the leaderstats to show how many parts have been cloned and randomly spawned throughout the baseplate, or “lorbs” in this case.

When I join the game the leaderstats show no value at all, when I set the value to a normal number in the leaderstats script it shows.

I’ve looked for solutions on here but I can’t seem to find any, I’ve tried to work it out myself, but I’m very stuck.

Let me know if you need more explanation!

Module (Workspace):

local Folder = Instance.new("Folder")
local Numbers = 1

while true do
	wait(.001)
	Numbers = Numbers + 1
	print(Numbers)
	
	--LOCAL--
	
	local lorb = game.ReplicatedStorage.Lorb
	local lorbclone = lorb:Clone()
	local randompart1 = math.random(-500,500)
	local randompart2 = math.random(50,500)
	local randompart3 = math.random(-500,500)
	local randomrot = math.random(-360,360) 
	
	--OTHER--
	
	Folder.Parent = workspace
	lorbclone.Name = "Lorb"
	lorbclone.Parent = Folder
	lorbclone.CanCollide = true
	
	lorbclone.Position = Vector3.new(randompart1,randompart2,randompart3)
	lorbclone.Rotation = Vector3.new(randomrot,randomrot,randomrot)
	

end

Local (StarterPlayer):

require(workspace.ModuleScript)

Leaderstats (ServerScripts):

local Players = game:GetService("Players")
local module = require(workspace.ModuleScript)

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local lorbs = Instance.new("IntValue")
	lorbs.Name = "lorbs"
	lorbs.Value = module.Numbers
	lorbs.Parent = leaderstats
end

Players.PlayerAdded:Connect(onPlayerJoin)

To index the variable Numbers from the module you should first return it, but I suggest you to use a table. Since you have an infinite loop, you can create another thread with task.defer() so the script doesn’t get stuck and returns the table.

ModuleScript:

local Folder = Instance.new("Folder")
local ModuleInfo = {
	Numbers = 1
}

task.defer(function()
	while true do
		wait(.001)
		ModuleInfo.Numbers += 1
		print(ModuleInfo.Numbers)

		--LOCAL--

		local lorb = game.ReplicatedStorage.Lorb
		local lorbclone = lorb:Clone()
		local randompart1 = math.random(-500,500)
		local randompart2 = math.random(50,500)
		local randompart3 = math.random(-500,500)
		local randomrot = math.random(-360,360) 

		--OTHER--

		Folder.Parent = workspace
		lorbclone.Name = "Lorb"
		lorbclone.Parent = Folder
		lorbclone.CanCollide = true

		lorbclone.Position = Vector3.new(randompart1,randompart2,randompart3)
		lorbclone.Rotation = Vector3.new(randomrot,randomrot,randomrot)


	end
end)

return ModuleInfo

From here you can just index .Numbers from the module as you were doing, to make sure it constantly updates, create another loop which updates every certain time. Adjust the time to your preference.

ServerScript:

local Players = game:GetService("Players")
local module = require(workspace.ModuleScript)

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local lorbs = Instance.new("IntValue")
	lorbs.Name = "lorbs"
	lorbs.Parent = leaderstats
	
	while true do
		task.wait(1) -- time it takes to update
		lorbs.Value = module.Numbers
	end
end

Players.PlayerAdded:Connect(onPlayerJoin)
1 Like

Sorry! I’ve only just seen this and it works, thank you so much! I was wondering if you could explain returning to me a bit more, I get really confused when it comes to returning

Sure, return is commonly used in scoped statements or modules, to return a value from a function/module or to stop everything that happens after the return in the current scope.

Returning in functions and modules looks like this:

-- Function
local function add(n1, n2)
    return n1 + n2
end

print(add(10, 10)) -- 20
-- Module
local Number = 2

return Number -- when requiring the module, it will be equal to the variable's number (2).

Return does also stop the rest of things in the script scope to happen, that’s why it always has to go at the end when returning values. Here’s an example:

local VariableThatStops = true

if VariableThatStops then
    return
end

print("This will print only if VariableThatStops is false.")

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