In that case, it’s possible that the GunAim part doesn’t exist in the script.Parent object. You can add a check to see if the part exists before trying to access its CFrame property:
script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if inorout == "In" then
if script.Parent:FindFirstChild("GunAim") then
game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
else
print("GunAim part not found")
end
else
game.Workspace.CurrentCamera.CameraSubject = hum
end
end)
This will print an error message to the output window if the GunAim part is not found in the script.Parent object, which can help troubleshoot the issue further.
He has stated that there are no errors, if a part doesn’t exist and you tried to get it’s cframe, it would throw a big fat error telling you that the part is nil, that doesn’t mean you shouldn’t use the code if there is a risk that the gun may have it’s welds removed / damaged and the part deleted
If that is the case, then I think the roblox engine is prioritizing the humanoid over the part, changing the cframe but still giving the humanoid the camera.
It’s possible that the camera’s view is being obstructed by something in the game world. You can try changing the FieldOfView property of the camera to see if the view changes:
script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if inorout == "In" then
game.Workspace.CurrentCamera.FieldOfView = 70
game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
else
game.Workspace.CurrentCamera.FieldOfView = 70
game.Workspace.CurrentCamera.CameraSubject = hum
end
end)
Setting the FieldOfView property to a value of 70 will give you a wider view, which might make it easier to see what’s obstructing the camera’s view. If this doesn’t work, you can try changing the value to a different number to see if it has any effect.
If @tahajjudnights 's idea doesn’t work, try setting the camera’s subject to nil.
script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if inorout == "In" then
game.Workspace.CurrentCamera.CameraSubject = nil
game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
else
game.Workspace.CurrentCamera.CameraSubject = hum
end
end)
Doesn’t seem like an issue in that way. If it helps I can set the CameraType to Scriptable and it will put the camera in the CFrame of GunAim, but just not when GuinAim Moves.
If that’s the case, set the camera’s type to scriptable when you want to set it to gunaim, and change it back if not, and if it’s not following the part it could not be running more then once.
It sounds like you may need to set the CameraType property to Scriptable to allow the camera to be positioned at the CFrame of the GunAim part. Here’s how you can modify the code to set the CameraType property:
script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if inorout == "In" then
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
else
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Follow
game.Workspace.CurrentCamera.CameraSubject = hum
end
end)
With this change, when inorout is equal to "In" , the camera’s CameraType property will be set to Scriptable , and the camera’s CFrame will be set to the CFrame of the GunAim part. When inorout is not equal to "In" , the camera’s CameraType property will be set to Follow , and the camera will follow the player’s character.
DevforumTestModel.rbxm (367.6 KB)
You can set the camera subject to the aimpart of it and it will work thing is you need to script the other part of it that makes it in first person and stuff and where it looks at.
also put it in StarterCharacterScripts
If setting the CameraSubject property to nil doesn’t work, you can try using a different method to return the camera to its original position. One approach is to store the original CFrame of the camera, and then use that CFrame to restore the camera to its original position:
local originalCameraCFrame = game.Workspace.CurrentCamera.CFrame
script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
if inorout == "In" then
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
else
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
game.Workspace.CurrentCamera.CFrame = originalCameraCFrame
end
end)
With this change, when inorout is not equal to "In" , the camera’s CameraType property will be set to Custom , and the camera’s CFrame will be set to the originalCameraCFrame value. This should return the camera to its original position.