How to target multiple objects in this script properly?

I have this script that is meant to target a particle in a model and change its speed with a slider.

But I am struggling to make it so it changes multiple particles.

I even tried putting the for i,v code to see if it worked, but it made other parts of the script stop working, or not showing a result.

-- Static variables

-- -- UI-related
local slider : GuiObject = script.Parent.Bar
local knob : TextButton = slider:FindFirstChild("Knob") :: TextButton
local previousSliderValue = 0
local sliderValue = 0 -- A multiplier between 0 to 1.

-- -- Target-related
local fountains = {
workspace.RingBTMCenter.Fountain1.cannon.core.fountain,
workspace.RingBTMCenter.Fountain2.cannon.core.fountain,
workspace.RingBTMCenter.Fountain3.cannon.core.fountain,
workspace.RingBTMCenter.Fountain4.cannon.core.fountain,
workspace.RingBTMCenter.Fountain5.cannon.core.fountain,
workspace.RingBTMCenter.Fountain6.cannon.core.fountain,
workspace.RingBTMCenter.Fountain7.cannon.core.fountain,
workspace.RingBTMCenter.Fountain8.cannon.core.fountain
}
local speedRange = Vector2.new(0, 90) -- Set the minimum and maximum speed here. (MinimumValue, MaximumValue)

-- -- Services
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

-- -- Dynamic variables
local mousePositionOnClick = Vector2.zero -- Starting from the point where the mouse clicked.
local mousePositionOnMove = Vector2.zero -- Updates every mouse move.

local trackingConnection : RBXScriptConnection
local isTrackingMouse = false
local isMouseOverKnob = false

-- Functions

-- -- A linear interpolation (Lerp) function for speed.
local function GetNewSpeed(Value : number) : number
	return speedRange.X + (speedRange.Y - speedRange.X) * math.clamp(Value, 0, 1)
end

-- -- Slides with the mouse.
local function UpdateSlider(Value : number)
	knob.Position = UDim2.new( -- Retain most of its position minus X.Scale.
		UDim.new(Value, knob.Position.X.Offset),
		knob.Position.Y
	)
end

-- -- Get the "delta" between the initial position of click to the current mouse position.
local function GetDelta()
	return mousePositionOnMove - mousePositionOnClick
end

-- -- Start tracking the mouse upon pressing.
local function StartTrackingMouse()
	if isTrackingMouse then
		return
	end

	mousePositionOnClick = UserInputService:GetMouseLocation() -- Set initial mouse position.

	trackingConnection = UserInputService.InputChanged:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.MouseMovement then
			local barCurrentSize = slider.AbsoluteSize.X
			mousePositionOnMove = UserInputService:GetMouseLocation()

			local delta = GetDelta()
			local scaleOffset = delta.X / barCurrentSize
			local newPosition = math.clamp(previousSliderValue + scaleOffset, 0, 1)

			sliderValue = newPosition
			fountains.Speed = NumberRange.new(GetNewSpeed(newPosition))

			UpdateSlider(newPosition)

			-- This just updates the textbutton's text. You can remove this safely if you don't use a textbutton, or you just don't want text.
			knob.Text = tostring(math.floor((newPosition * 100)) / 100)
		end
	end)

	previousSliderValue = sliderValue

	isTrackingMouse = true
end

-- Stop tracking the mouse upon user releasing it.
local function StopTrackingMouse()
	if not isTrackingMouse then
		return
	end

	trackingConnection:Disconnect()

	isTrackingMouse = false
end

knob.MouseButton1Down:Connect(StartTrackingMouse)
UserInputService.InputEnded:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
		StopTrackingMouse()
	end
end)

At the top of the script I put each fountain that’s in the RingBTMCenter folder, but didn’t end up how I intended.

1 Like

If anyone knows how I can fix this, I would be grateful.
If this is possible to do, a detailed explanation would be appreciated.

I am not entirely sure of your question, but if you are asking how to iterate through an array of particles, then use this:
local myParticles = {particle1, particle2, particle3} --a table of particles
for i,v in pairs(myParticles) do --iterate through the table
print(i, v) --print the current index and current item
end

1 Like

Nevermind!

I just had to make it for i, fountains in pairs(fountains) do

Just forgot that “v” was a variable meant to be changed.

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