ModuleScript: How to split up variables for 2 different scripts so that they don't intervene with eachother?

  1. What do you want to achieve?
    I have multiple objects that I call Rod Controls, each of them have a logic script for their behaviour. I want to be able to duplicate these dynamically without having to change anything in their script, so I make the logic script reference a ModuleScript for the methods.

  2. What is the issue?
    The objects have certain buttons and displays that they use which they save in a variable. Doing this worked with one object, however with 2 objects/identical scripts, one ends up using the buttons of the other, aka they share variables.

grafik
Fun fact: Their scripts start at randomly different times, so it’s a 50/50 which machine’s buttons are being used. Edit: Another side-effect that is that the “working” controller now works at double efficeny as both scripts are focused on it.

  1. What solutions have you tried so far?

I haven’t found a proper solution for this. I could remove the variable part and reference the objects directly instead (script.Parent.Button_Eject.Material instead of ejectButton.Material), however that would bloat the code (I don’t know about the theoretical performance change), haven’t tried that out yet.

local DR2Scripts_ControlRod = {}

function DR2Scripts_ControlRod.initObjects(scriptLocation) -- Initalize Objects required
	displayNumber = scriptLocation.Parent.DisplayInjection.SurfaceGui.TEXT
	injectButton.clickInject.MouseClick:connect(onClickInject)
...
end
...
function onClickInject()
	injectButton.Material="Neon"
end
...
return DR2Scripts_ControlRod
local DR2Scripts_ControlRod = require(script.Parent.Parent.Parent.ScriptDatabase.DR2Scripts_ControlRod)

DR2Scripts_ControlRod.initObjects(script)
DR2Scripts_ControlRod.action()

Hi, you could pass another parameter through the modulescript function telling it which rodcontroller to run on.

1 Like

I’m not sure yet how I would do that, can you give me an example?

I’ve managed to fix this issue by making all Controller/Object unique variables (like buttons) local.

After some testing I found out local variables and functions were not affected by this issue so I restructured my code to have everything local. It looks like this now:

local DR2Scripts_ControlRod = {}


function DR2Scripts_ControlRod.action(scriptLocation) -- Action Loop
	local displayNumber = scriptLocation.Parent.DisplayInjection.SurfaceGui.TEXT
	local displayStatus = scriptLocation.Parent.DisplayStatus.SurfaceGui.TEXT
	local rodInjection = 100
	local status = "Stay"

	local injectButton = scriptLocation.Parent.Button_Inject
	local stayButton = scriptLocation.Parent.Button_Stay
	local ejectButton = scriptLocation.Parent.Button_Eject
	--clickPanic = script.Parent.Parent.Button_Panic.clickInject--One button all LogicScripts accsess
	
	local function onClickInject()
		injectButton.Material="Neon"
		status="Inject Rod"
		wait(0.15)
		injectButton.Material="Plastic"
	end

	local function onClickStay()
		stayButton.Material="Neon"
		status="Stay"
		wait(0.15)
		stayButton.Material="Plastic"
	end

	local function onClickEject()
		ejectButton.Material="Neon"
		status="Eject Rod"
		wait(0.15)
		ejectButton.Material="Plastic"
	end
	
	injectButton.clickInject.MouseClick:connect(onClickInject)
	stayButton.clickStay.MouseClick:connect(onClickStay)
	ejectButton.clickEject.MouseClick:connect(onClickEject)
	--clickPanic.MouseClick:connect(onClickInject)
	
	while true do
		while status == "Inject Rod" and rodInjection<100 do
			rodInjection=rodInjection+1
			displayNumber.Text=rodInjection
			displayStatus.Text = "Injecting Rod"
			if status == "Inject Rod" and rodInjection==100 then
				status="Stay"
			end
			wait(1.2)
		end
		while status == "Eject Rod" and rodInjection>0 do
			rodInjection=rodInjection-1
			displayNumber.Text=rodInjection
			displayStatus.Text = "Ejecting Rod"
			if status == "Inject Rod" and rodInjection==0 then
				status="Stay"
			end
			wait(1.2)
		end
		while status == "Stay" or status == "Inject Rod" and rodInjection>99 or status == "Eject Rod" and rodInjection<1 do --All cases are covered to avoid endless loop
			displayStatus.Text = "Holding Position"
			wait(1.2)
		end
	end
end

print("DR2 Control Rod Script loaded")

return DR2Scripts_ControlRod

Thanks to Grayseon for the attempted help, even if I didn’t use his solution it was still motivational