#first person in a specific area
1 messages · Page 1 of 1 (latest)
use touched/touchended events to swap the players camera mode
alr lemme try
i made this script but when the user leaves the area it doesnt revert back to 3rd person is there any errors
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
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