How do i reference multiple objects in a variable

  1. Im trying to make a variable equal two objects for a script using the touch function to modify a value
    in a GUI

  2. ive tried using or for it but it doesnt work

  3. ive looked on the dev hub and it shows that you can use iterating with i pairs but i dont understand
    how that works and im not sure if it would work with the touch function

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

im only pasting the parts of the script that do the function described earliers as the rest of the script is just speed calculations

Variables

-- bin = script.Parent
Sensorpart = bin.Sensor or bin.Parent._4.Sensor
eng = { }
st = bin.VehicleSeat
msp = script.MaximumSpeed.Value
st.MaxSpeed = msp
mrs = script.MaximumReverseSpeed.Value --> Maximum Reverse Speed
spt = script.SpeedPerThrottle.Value --> Speed Per Throttle. This is how much each notch on the throttle changes
mxt = math.ceil(msp / spt) --> This is the maximum throttle
crt = 0 --> Current throttle
csd = 0 --> Current speed
cdr = 1 --> Current direction
drive = false --> Is the train driving. False = not driving, true = driving
plr = nil --> This will change to the current driver
DirTable = {"Reverse", "Neutral", "Foward"}
TrtStp =1 --> This is used so there is a second or 2 gap in between changing the throttle.

code that modify the gui

Sensorpart.Touched:Connect(function(child)
				if child:IsA('Part') then
					task.wait(0.5)
					if child.Name == "V30" then
						h2.MainDriving.Manipulateur.Max.Text = "30"
					elseif child.Name == "V50" then
						h2.MainDriving.Manipulateur.Max.Text = "50"
					elseif child.Name == "V60" then
						h2.MainDriving.Manipulateur.Max.Text = "60"
					elseif child.Name == "V80" then
						h2.MainDriving.Manipulateur.Max.Text = "80"
					elseif child.Name == "V90" then
						h2.MainDriving.Manipulateur.Max.Text = "90"
					elseif child.Name == "V100" then
						h2.MainDriving.Manipulateur.Max.Text = "100"
					end
				end
			end)

What do you mean by “reference multiple objects in a variable”?

Anyway, if you are talking about “Sensorpart” having two objects, yes, you probably should use for loops.

An example of a "for i,v in pairs(table) loop

local randomTable = {"regularpotato","friedpotato","bakedpotato"}
--Lets say we wanted to print each thing inside.
for i,potatotype in pairs(randomTable) do -- basically i is the index or number in a table. (Ex: "regularpotato" has an index of 1, "friedpotato" has an index of 2 and so on.). potatotype is the object/variable itself (if theres an index of 1 it will be "regularpotato" and so on.
     print(i,potatotype)
end
-- Would print
--1 "regularpotato"
--2 "friedpotato"
--3 "bakedpotato"

Which would lead you to make something like:

local bin = script.Parent
local SensorParts = {bin.Sensor,bin.Parent._4.Sensor}

for i,SensorPart in pairs(SensorParts) do
   SensorPart.Touched:Connect(function(Child) -- And yes, .Touched works on for i,v loops as long as it is a object instance.
      -- run your code
   end
end

Also, just a good habit though, make sure your variables are local (adding a local before listing them) as it is just neater and usually better.

Let me know if this helps or if you have any problems with it!

1 Like

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