Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to Make A Light Turn On When You Step Into A Certain Area And Off Again When You Leave?

I am making a Diorama of sorts and wanted to make a light that activates when you get close enough, and when you leave the area, it shuts back off. I was assuming it'd be the same way you would make detection areas to play music or sounds in only certain areas by hooking up two different blocks to one script, but I have no clue what I'm doing and have been trying to get this to work for the past day. I have no prior use of LUA or the Script function in general, and I can't find anything around that can help.

1 comment

0

You will never have a prior use of LUA, but you can have a prior use of Lua.

1 answer

✓ Accepted answer

You can use this script but instead of getting the position of the camera you can get the players position.

while wait() do
	for q = 1, #Zones do
		local cameraX = cam.CFrame.X
		local cameraY = cam.CFrame.Y
		local cameraZ = cam.CFrame.Z
		local Hitparts = Zones[q].Hitbox:GetChildren()
		for i = 1, #Hitparts do
			local PartPos = Hitparts[i].Position
			local PartSizeX = Hitparts[i].Size.X
			local PartSizeY = Hitparts[i].Size.Y
			local PartSizeZ = Hitparts[i].Size.Z

			local RootPartX = char.HumanoidRootPart.CFrame.X
			local RootPartY = char.HumanoidRootPart.CFrame.Y
			local RootPartZ = char.HumanoidRootPart.CFrame.Z
			
			if cameraX > PartPos.X-PartSizeX/2 and cameraX < PartPos.X+PartSizeX/2 and cameraY > PartPos.Y-PartSizeY/2 and cameraY < PartPos.Y+PartSizeY/2 and cameraZ > PartPos.Z-PartSizeZ/2 and cameraZ < PartPos.Z+PartSizeZ/2 then
				if not inside then
					inside = true
					outside = false
				end
			else
				if not outside then
					outside = true
					inside = false
				end
			end
		end
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.