I want to check if player’s camera is in a part and this script works but it is keep repeating how could I prevent it from repeating?
this script isn’t mine.
repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
local Camera = game.Workspace.CurrentCamera
local function CheckPointinPart(point, part)
point = part.CFrame:pointToObjectSpace(point)
return math.abs(point.X) <= part.Size.X / 2
and math.abs(point.Y) <= part.Size.Y / 2
and math.abs(point.Z) <= part.Size.Z / 2
end
Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
if CheckPointinPart(Camera.CFrame.p, game.Workspace.Part) then
print("The Camera is within the Part!")
end
end)
1 Like
What do you mean by ‘keep on repeating’?
1 Like
I’m also wondering what part of your script you want to keep from repeating. However, here’s a solution to stop the “GetPropertyChangedSignal” part of the script.
You can “wrap” it it in a conditional statement, and have a boolValue on the outside of it. Then from the inside, once you’ve had it run once, you can change the boolValue to the opposite of the condition for the while loop. Like this:
local Camera = game.Workspace.CurrentCamera
local function CheckPointinPart(point, part)
point = part.CFrame:pointToObjectSpace(point)
return math.abs(point.X) <= part.Size.X / 2
and math.abs(point.Y) <= part.Size.Y / 2
and math.abs(point.Z) <= part.Size.Z / 2
end
local ranOnce = false
Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
if ranOnce == false then
if CheckPointinPart(Camera.CFrame.p, game.Workspace.Part) then
print("The Camera is within the Part!")
ranOnce = true
end
end
end)
This should only work once, then no longer - without repeating.
But let us know if this isn’t what you meant. It’s not very clear the way you worded it.
1 Like
GetPropertyChangedSignal
for camera movements is most likely will triggered as it moves. What @MJTFreeTime should works if you only want to detect it once. However I would do it with :Disconnect()
rather than keep it connected but do nothing.
local connection
connection = Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
if CheckPointinPart(Camera.CFrame.p, game.Workspace.Part) then
print("The Camera is within the Part!")
connection:Disconnect()
end
end)
This will free up memory used by the :Connect()
previously.
If your intention is to detect the event multiple times, like when the camera is IN/OUT
the part without repeating the same event twice. You can do it like this.
local cameraIsInsidePart = false
Camera:GetPropertyChangedSignal("CFrame"):Connect(function()
if CheckPointinPart(Camera.CFrame.p, game.Workspace.Part) then
if not cameraIsInsidePart then
cameraIsInsidePart = true
print("The Camera is within the Part!")
end
else
if cameraIsInsidePart then
cameraIsInsidePart = false
print("The Camera is NOT within the Part!")
end
end
end)
"The Camera is within the Part!"
should only trigger when camera enter the part and not repeating WHILE camera is inside.
"The Camera is NOT within the Part!"
should only trigger when the camera leave the part area and not repeating WHILE camera is outside.
And so on.
2 Likes