How can I get variable from module script?

local script:

local x = require(game:GetService("ReplicatedStorage").ModuleScript) 

print(x.target1)

x.CheckAndDeploy()

print(x.target1)

x.hi()

--Print:

--nil

--nil

--12

module script:

local ClapSkill = {
	
}

local target1 = 1

function ClapSkill.CheckAndDeploy()
	target1 = 12
	
	
end

function ClapSkill.hi()
	print(target1)


end



return ClapSkill
2 Likes

Why not just insert the variable in the table?

local ClapSkill = {
	
}

ClapSkill.target1 = 1

function ClapSkill.CheckAndDeploy()
	ClapSkill.target1 = 12
	
	
end

function ClapSkill.hi()
	print(ClapSkill.target1)


end



return ClapSkill
2 Likes

Place the variable in the table that the module is returning.

Example:

local module = {
      Target1 = 1
}

return module
3 Likes

Correct me if Im wrong.
Modules are called as “function” so if you called a function like

local returns = SomemoduleFunction()

it will then stored a “returns” variable of SomemoduleFunction()

2 Likes

is it possible to make variable that can use on all module something like global variable?

1 Like

Just insert the variable in the table

local Module = {}

Module.target1 = 10

return Module

You can always require the same in every script

local a = require(modulescriptlocation)

print(a.target1)
1 Like

Im trying to make skill that need to change some variable in module 1 and use in module2
(all module share same variable what I mean)

1 Like

That can still be required in module2 though. It will work.

1 Like