How would I go on about traveling lights?

Hello, how would I go on about making a traveling light?

E.g:
(video from OAS3)

  • p.s: im talking about the spotlights, they go from a top to bottom order.

I tried using for loops, it did work, but it turned off random spotlights, and not from a top to bottom style order.

1 Like

You could try to use tween service to change their orientation

You should use your for loop, but sort the table by whatever criteria you want. For example, by Y position:

local lights = something:GetChildren()

local function SortFunc(a, b)
  return a.Position.Y < b.Position.Y -- your sorting criteria
end

table.sort(lights, SortFunc)

-- now do your for loop
2 Likes

I tested this on another place, it worked. But when I test it in the main game, it will not work, I have tried using the table, etc.

Code;

task.wait(1)
local beams = {}
for i,v in pairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
	if v:IsA("Beam") then
		table.insert(beams, v)
	end
end

local function sort(a, b)
	return a.Position.X < b.Position.X
end

table.sort(beams, sort)

local function tween(object, goals, timer)
	local tween = game:GetService("TweenService"):Create(object, TweenInfo.new(timer, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), goals)
	tween:Play()
	tween.Completed:Wait()
	tween:Destroy()
end

script.Parent.MouseClick:Connect(function(plr)
	if plr.UserId == 2407693045 then
			for _, l in pairs(beams) do
			pcall(function()
				if l:IsA("Beam") then
					l.Enabled = true
					task.wait(.001)
				end
			end)
		end
				tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.fromRGB(175, 175, 175)}, .25)
				task.wait(.025)
				tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.new(0,0,0)}, .25)
		else
			plr:Kick("DENIED")
	end
end)

Change pairs to ipairs to iterate through in order.

Just now tried that. Same error…

new code;

task.wait(1)
local beams = {}
for i,v in pairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
	if v:IsA("Beam") then
		table.insert(beams, v)
	end
end

local function sort(a, b)
	return a.Position.X < b.Position.X
end

table.sort(beams, sort)

local function tween(object, goals, timer)
	local tween = game:GetService("TweenService"):Create(object, TweenInfo.new(timer, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), goals)
	tween:Play()
	tween.Completed:Wait()
	tween:Destroy()
end

script.Parent.MouseClick:Connect(function(plr)
	if plr.UserId == 2407693045 then
			for _, l in ipairs(game.Workspace:WaitForChild("GroundSpotLights"):GetDescendants()) do
			pcall(function()
				if l:IsA("Beam") then
					l.Enabled = true
					task.wait(.001)
				end
			end)
		end
				tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.fromRGB(175, 175, 175)}, .25)
				task.wait(.025)
				tween(game.Workspace:WaitForChild("Lighting"), {Color = Color3.new(0,0,0)}, .25)
		else
			plr:Kick("DENIED")
	end
end)

So the way to approach this is as following.

  • Make a Lights folder.
  • Make a Childfolder for each wave/line of light you want tweened. Name each Childfolder in order, from 1 to how many lines/waves of lights you have.
  • Put one or multiple lights into these Childfolders.

and then you just loop through it all!

local LightsFolder = workspace:WaitForChild("LightsFolder")

for Count = 1, #LightsFolder:GetChildren() do
	local ChildFolder = LightsFolder:FindFirstChild(Count)
	if ChildFolder then
		for _, Light in pairs(ChildFolder:GetChildren()) do
			if Light:IsA("Pointlight") or Light:IsA("SurfaceLight") or Light IsA("Spotlight") then
				-- TWEEN STUFF for each light!
			end
		end
	end
end

and if you want the loop to go backwards, just do this

for Count  = #LightsFolder:GetChildren(), 1, -1 do
	local ChildFolder = LightsFolder:FindFirstChild(Count)
	if ChildFolder then
		for _, Light in pairs(ChildFolder:GetChildren()) do
			if Light:IsA("Pointlight") or Light:IsA("SurfaceLight") or Light IsA("Spotlight") then
				-- TWEEN STUFF for each light!
			end
		end
	end
end

Personally I use this method a lot, if I want to sort tweened stuff. I could go by sorting a table by any value like position.y, but with my method, you can go way beyond and make a huge neon palm tree, that will turn on/off the lights in the order you want them to.

Just tried that, it couldn’t get the children of an index. So I then tried LightsFolder:GetChildren()). It worked, but it wasn’t in order…

Tried testing that on a place, it works, but in my game, it doesn’t. So I will go ahead and re write the code to see if it will fix.

This should use the sorted beams table. It shouldn’t make a new one—then you’ve lost all the sorted-ness!

for _, l in pairs(beams) do

I know, I re-wrote the code now, and it works!

Thanks

I wrote a part of it wrong, it’s updated now.