We use cookies on this site to enhance your user experience

Oct 01 2018, 2:32 AM PST

This code sample causes a part to blink its Reflectance and a PointLight every time it is touched. It uses a pattern that prevents multiple concurrent function calls from fighting with each other.

local part = script.Parent

-- Add a light
local pointLight = Instance.new("PointLight", part)
pointLight.Brightness = 0
pointLight.Range = 12

local touchNo = 0
local function blink()
	-- Advance touchNo to tell other blink() calls to stop early
	touchNo = touchNo + 1
	-- Save touchNo locally so we can tell when it changes globally
	local myTouchNo = touchNo
	for i = 1, 0, -.1 do
		-- Stop early if another blink started
		if touchNo ~= myTouchNo then break end
		-- Update the blink animation
		part.Reflectance = i
		pointLight.Brightness = i * 2