Is it possible to use Variable from another script Without ModuleScript

for example
so for example I have Function in another script

local Function PartFucn()
print(“it get”)
end

in another script

wait(5)
PartFucn()
1 Like

you could define global variables using _G. before the variable name, but module scripts are usually a better alternative
example:
script 1:

function _G.globalFunction()
    print("test")
end

script 2:

wait(5)
_G.globalFunction()
1 Like

I use the module a bit difficult, because the script is not auto correct, and I have to write the child script manually.

you could try using global variables like this

1 Like

I want like, when writing a script get auto correct like, script.parent
when typing par after the script then Lua will auto correct changing par to Parent in one enter

but somehow the script module can’t do that

it can, if you formatting it this way:

local function clonepart(part:BasePart)
-- part is defined as a basepart therefor will autofill when scripting.

-- useful fact putting colon and then what u want it to be like passing this: Table:table 
--then it will be detected as table
end

if u wrote part.Par in the fucntion it would autofill

is it possible that the Module look like this

local text = "txt here"
local text2 = "txt here"
local model = script:GetChildren("Part")

local function test()
	print(text)
end

local function test2()
	print(text2)
end

local function PartoWSpace()
	local part = model
	part.parent = workspace
end

local module = {test()}
local module2 = {test2()}
local moduleTOws = {PartoWSpace()}


return module

so on the other script i use it like this

require(script.ModuleScript)
math.random( module, module2, moduleTOws)

This is not a method. script:GetChildren returns a table of the script’s children; you cannot use a specific child using this method.

It should be:

local model = script:FindFirstChild("Part")

That’s not how that works; you need to return everything you want scripts to access.

-- In the module script
return test, test2, PartoWSpace
-- in another script
-- you define the variables in the order you return them
local test, test2, PartoWSpace = require(path.to.ModuleScript)
local list = {test, test2, PartoWSpace}
local f = list[math.random(#list)] -- i'm assuming you want to get a random function?

f() -- call the random function

i did and it has an error
Module code did not return exactly one value

local test, test2, PartoWSpace = require(game.ReplicatedStorage.testThing) -- where it gets error
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
while true do
	local functions = {test, test2, PartoWSpace}
	local randomFunction = functions[math.random(#functions)]

	if sfx.accept:Play() then
		wait(3)
		print("less than 9700")
	
	else
		print("more than 9700")
		wait(5.7)
	end
	randomFunction()
end

image
dont mind the redline

return function()
    return test, test2, PartoWSpace
end
-- other script
local test, test2, PartoWSpace = require(game.ReplicatedStorage.testThing) () -- call the function that was returned, to fetch the values
1 Like

The module can’t return more than one object:

--~ ModuleScript ~--
local data0, data1, data2 = {}, "string", false
return data0, data1, data2

local d0, d1, d2 = require(path.to.module)
print(d0, d1, d2) -- prints `{} nil nil`

I’d suggest you to make it into a table or class.

Ok, so the answer is yes and no.

Do not use _G, just take some steps back and stay away from it. =P For your sanity and the sanity of any who work with you.

My assumption is, you would like to have a more ambiguous way to grab values from elsewhere.

Look up “observer pattern” that is what you are looking for. You will need a module, but the modules job will be to act as your go betweener for communicating between things. So instead of the need to require the value directly from a specific module, you have one module that manages all of your subscriptions, publishes.

There are some examples out there too, but that’s likely what your looking to do.

In theory, if you wanted to be lazy, you could have a bindable function you could ref and call, and pass through the Id of the variable you want. The hook up for the bind function would then recall it for you. But again, you REALLY want some aspect of the observer pattern implementation.

Be careful though, decoupling code is very powerful but comes with great responsibility in organizing and being clear with your commenting and structure or you can find yourself in a world full of troublesome code.

1 Like

Why would you do this ?
To be clear I was talking about how he defined his.

What is below would do the same thing without needing a tuple.

local helperModule = {}

helperModules.test = function() end

helperModule.test2 = function() end

helperModule.PartoWSpace = function() end

helperModule.BigTest = function()
     local test = helperModule.test()
    local test2 = helperModule.test2()
     local PartoWSpace = helperModule.PartoWSpace()
    return test, test2, PartoWSpace
end
return helperModule

1 Like

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