#first person in a specific area

1 messages · Page 1 of 1 (latest)

fresh hill
#

im trying to make it so that when a user is inside of or touching a specific part they are forced into first person and they go back to normal when they exit the area

devout bear
#

use touched/touchended events to swap the players camera mode

fresh hill
#

alr lemme try

fresh hill
#

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local zonePart = workspace:WaitForChild("MusicZoneUnc")

local inZone = false
local originalCameraMode = player.CameraMode

local function enterZone()
if inZone then return end
inZone = true
originalCameraMode = player.CameraMode
player.CameraMode = Enum.CameraMode.LockFirstPerson
end

local function leaveZone()
if not inZone then return end
inZone = false
player.CameraMode = originalCameraMode
end

local function stillTouching()
local character = player.Character
if not character then return false end

for _, part in pairs(character:GetDescendants()) do
    if part:IsA("BasePart") and part:IsTouching(zonePart) then
        return true
    end
end
return false

end

zonePart.Touched:Connect(function(hit)
local character = player.Character
if character and hit:IsDescendantOf(character) then
enterZone()
end
end)

zonePart.TouchEnded:Connect(function(hit)
local character = player.Character
if character and hit:IsDescendantOf(character) then

    task.delay(0.1, function()
        if not stillTouching() then
            leaveZone()
        end
    end)
end

end)

#

the part is called MusicZoneUnc btw

devout bear
#

i mean it looks pretty good, the culprit could be that touchend on a localscript is unreliable. and if u set CanCollide = false on the zone to let the player walk through it, u often wont get any touch end events at all. a more reliable way to do it is to poll each frame whether the character is overlapping the zone, and switch in or out only when that state actually changes.

#

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local zonePart = workspace:WaitForChild("MusicZoneUnc")

local inZone = false
local originalCameraMode = player.CameraMode

local function checkZone()
local char = player.Character
if not char then return end

-- scan all BaseParts in the character to see if ANY of them are still touching the zone
local touching = false
for _, part in ipairs(char:GetDescendants()) do
    if part:IsA("BasePart") and part:IsTouching(zonePart) then
        touching = true
        break
    end
end

if touching and not inZone then
    -- entered the zone
    inZone = true
    originalCameraMode = player.CameraMode
    player.CameraMode   = Enum.CameraMode.LockFirstPerson
elseif not touching and inZone then
    -- left the zone
    inZone = false
    player.CameraMode = originalCameraMode or Enum.CameraMode.Classic
end

end

-- poll every frame
RunService.Heartbeat:Connect(checkZone)

#

delete or disable ur old touched/touchended script. create a new localscript under starterplayerscripts, paste this code i made and make sure ur part is named exactly MusicZoneUnc and has CanTouch = True