--local BulbSet ={}

local children = model:GetChildren()
for i = 1, #children do
	if children[i].Name == "Light" then
		table.insert(LightSet, children[i])
		table.insert(LightStartingBrightnesses, children[i].SpotLight.Brightness)
	end
end

local function PlaySound()
	model["Sound"]:Play()
end

local function evalNS(ns, time)
	-- If we are at 0 or 1, return the first or last value respectively
	if time == 0 then return ns.Keypoints[1].Value end
	if time == 1 then return ns.Keypoints[#ns.Keypoints].Value end
	-- Step through each sequential pair of keypoints and see if alpha
	-- lies between the points' time values.
	for i = 1, #ns.Keypoints - 1 do
		local this = ns.Keypoints[i]
		local next = ns.Keypoints[i + 1]
		if time >= this.Time and time < next.Time then
			-- Calculate how far alpha lies between the points
			local alpha = (time - this.Time) / (next.Time - this.Time)
			-- Evaluate the real value between the points using alpha
			return (next.Value - this.Value) * alpha + this.Value
		end
	end
end

local function touched(otherPart)
	if(otherPart.Name == "HumanoidRootPart" ) then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if(player) then
			trigger.CanTouch = false
			
			PlaySound()
			wait (.4)
			
			script.Parent.Light.ParticleEmitter.Enabled = true
			
			for i = 0,loopCount do
				local t = 0.0
				while t < 1.0 do
					for i = 1, #LightSet do
						--local randomized_t = math.round(t + i * 0.1)
						local spotlights = LightSet[i]:GetChildren()
						LightSet[i].Transparency = 1 - evalNS(trigger:GetAttribute("Flicker"), t)
						for j = 1, #spotlights do
							spotlights[j].Brightness = evalNS(trigger:GetAttribute("Flicker"), t) * 1 ---LightStartingBrightnesses[i]
						end
					end
					
					t += dt
					wait(0.01)
				end
			end
			
			for i = 1, #LightSet do
				LightSet[i].SpotLight.Brightness = .25 --LightStartingBrightnesses[i]
			end						
			
			wait(90)
			trigger.CanTouch = true
		end
	end
end

trigger.Touched:Connect(touched)


