Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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.

0
You will never have a prior use of LUA, but you can have a prior use of Lua. User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by
DevingDev 346 Moderation Voter
5 years ago

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
Ad

Answer this question