Variable from script to modulair script

You can write your topic however you want, but you need to answer these questions:

  1. I want to set a variable in the main script and use it in a modular script

  2. the modular script does not use the same variables as the main script

Mainscript
err,mess = pcall(function()
	for i,v in pairs(Fixtures:GetDescendants()) do
		--print("checking "..v.Name)
		if v:IsA("Model") and v:FindFirstChild("Setup") then
			warn("found a light")
			local Channels = require(v.Setup.Software)
			local Adress = Channels["Setup"]["Adress"]
			local Universe = tonumber(Channels["Setup"]["Universe"])
			--print(HttpService:JSONEncode(Channels))
			--print(Adress)
			--print(Universe)

			--setups universe
			if Universe < 10 then
				Universe = "U0"..Universe
			elseif Universe >= 10 then
				Universe = "U"..Universe
			end
			Universe = Universes:FindFirstChild(Universe)
			--setups channel
			for i,v in pairs(Channels) do
				i = tonumber(i) 
				if i then
					err,mess = pcall(function()
						local FChannel = i -- gets fixtures channel

						if  FChannel >=1 and FChannel<=9 then
							FChannel = ("C00"..FChannel)
						elseif FChannel>=10 and FChannel<=99 then
							FChannel = ("C0"..FChannel)
						elseif FChannel>=100 and FChannel<=512 then
							FChannel = ("C"..FChannel)
						else 
							print("Adress Isue")
						end	

						local Channel = i+Adress-1 -- real channel

						if  Channel >=1 and Channel<=9 then
							Channel = ("C00"..Channel)
						elseif Channel>=10 and Channel<=99 then
							Channel = ("C0"..Channel)
						elseif Channel>=100 and Channel<=512 then
							Channel = ("C"..Channel)
						else 
							print("Adress Isue")
						end	
						print("connected "..FChannel.." to "..Channel)
						getfenv()[FChannel] = Universe:FindFirstChild(Channel)

						Channel = Universe:FindFirstChild(Channel) -- search in universe

						Channel.Changed:Connect(function()err,mess = pcall(Channels[FChannel])
							if not err then
								erro(mess)
							end
						end
						)
						-- connects fixtures channel to real channel
					end)
				end
			end
		end
	end
end)
...

in the main script, I determine C001 to be the number value in the picture below

afbeelding

then in the module script, I want to use C001.Value to refer again to that number value

Module

local Software = {
	"red",
	"green",
	"blue",
}
Software["Setup"] = require(script.Parent)

fixture = script.Parent.Parent.Fixture


function onactiavte ()
	fixture.TiltMe.Light.Color = Color3.fromRGB(C001.Value,C002.Value,C003.Value)
	fixture.TiltMe.Light.SpotLight.Color = Color3.fromRGB(C001.Value,C002.Value,C003.Value)
	fixture.TiltMe.Light.Beam.Color = ColorSequence.new(Color3.fromRGB(C001.Value,C002.Value,C003.Value))
	fixture.TiltMe.Light.Beam.Transparency = NumberSequence.new{
		NumberSequenceKeypoint.new(0,1-(C001.Value+C002.Value+C003.Value)/(255*3)),
		NumberSequenceKeypoint.new(1,1),}
end

Software.C001 = function()
	onactiavte()
end

Software.C002 = function()
	onactiavte()
end

Software.C003 = function()
	onactiavte()
end

return Software

as i am making more lights i would like to keep the module script as empty as posible.

added the warning message
afbeelding

1 Like

I dont get what your asking you said in the title “variable from script to modular script” but at the end of your post you said you want to keep the module script as empty as possible, youll need to provide more information on what your really try to achieve.

example:

in my script i have
C001 = U01.C009

in my module script, I want to do
fixture.Light.Color = Color3.fromRGB(C001.Value,255,255) --sets the red value of a light

The error says: attempt to index nil with 'Value'
This means that you are doing nil.Value, which means C001 is nil, which means there is no U01.C009
(but U01 does exist, otherwise it would’ve errored at U01.C009)

in the main script i define C001 as U01.C009
somehow i want to transfer that to the module script

Oh. I completely failed to read your entire post, sorry.

A ModuleScript is entirely its own script, with different variables from what calls it; requiring a ModuleScript does not put its code into the Script where it was called. So the only way to pass variables around is to pass the values around when calling functions.

-- ModuleScript
-- ...
function onactiavte (C001, C002, C003)
	fixture.TiltMe.Light.Color = Color3.fromRGB(C001.Value,C002.Value,C003.Value)
	fixture.TiltMe.Light.SpotLight.Color = Color3.fromRGB(C001.Value,C002.Value,C003.Value)
	fixture.TiltMe.Light.Beam.Color = ColorSequence.new(Color3.fromRGB(C001.Value,C002.Value,C003.Value))
	fixture.TiltMe.Light.Beam.Transparency = NumberSequence.new{
		NumberSequenceKeypoint.new(0,1-(C001.Value+C002.Value+C003.Value)/(255*3)),
		NumberSequenceKeypoint.new(1,1),}
end

Software.C001 = function(C001, C002, C003)
	onactiavte(C001, C002, C003)
end

Software.C002 = onactiavte -- does the same as above

Software.C003 = onactiavte

return Software

Then you have to call Software.C001 with all these variables.

Or you can use getfenv(2) to get the environment of the script that’s using the module and get the C values from there, provided that they aren’t locals.

yeah, bummer, was hoping on an easy workaround

ty tho