Since these are both LocalScripts, and you only have one loop, you don't even need two scripts, you can simply combine them.
To change the CameraType, you can simply define the string, rather than write the "Enum"
The while true do loop requires a wait time (using wait(0) may break the script)
You can immediately connect the function when you define it.
The player's Character's Head is the default CFrame, so when the loop is disabled, this is what should be the new setting
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
script.BoolValue.Value = true
game.ReplicatedStorage.Cam.OnClientEvent:Connect(function()
script.BoolValue.Value = true
end)
game.ReplicatedStorage.Cam2.OnClientEvent:Connect(function()
script.BoolValue.Value = false
end)
while true do
if script.BoolValue.Value = true then
Camera.CameraType = "Scriptable"
Camera.CFrame = workspace.CameraPart.CFrame
else
Camera.CameraType = "Custom"
Camera.CFrame = Character.Head.CFrame
end
wait()
end
If you add a "BoolValue" -- in this case simply named "BoolValue" under the script, this will keep the loop in check --- this example has the Bool Automatically set to true.
Otherwise, if you want the loop to be broken, you can add a conditional event where upon being set by the Cam2 Client Event, the loop will break, if using the above script, you would add it under the "else" condition. This will make sure that the loop will not be run again, ever.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
script.BoolValue.Value = true
game.ReplicatedStorage.Cam.OnClientEvent:Connect(function()
script.BoolValue.Value = true
end)
game.ReplicatedStorage.Cam2.OnClientEvent:Connect(function()
script.BoolValue.Value = false
end)
while true do
if script.BoolValue.Value = true then
Camera.CameraType = "Scriptable"
Camera.CFrame = workspace.CameraPart.CFrame
else
Camera.CameraType = "Custom"
Camera.CFrame = Character.Head.CFrame
break
end
wait()
end
You can also change your loop to be a repeat loop based on a value or event
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
script.BoolValue.Value = true
wait()
game.ReplicatedStorage.Cam.OnClientEvent:Connect(function()
script.BoolValue.Value = true
end)
game.ReplicatedStorage.Cam2.OnClientEvent:Connect(function()
script.BoolValue.Value = false
end)
repeat
Camera.CameraType = "Scriptable"
Camera.CFrame = workspace.CameraPart.CFrame
wait()
until script.BoolValue.Value = false
Camera.CameraType = "Custom"
Camera.CFrame = Character.Head.CFrame
However, you can also just set the settings of the camera without the loop at all like so:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("CameraPart")
Camera.CameraType = "Scriptable"
Camera.CFrame = part.CFrame
game.ReplicatedStorage.Cam.OnClientEvent:Connect(function()
Camera.CameraType = "Scriptable"
Camera.CFrame = workspace.CameraPart.CFrame
end)
game.ReplicatedStorage.Cam2.OnClientEvent:Connect(function()
Camera.CameraType = "Custom"
Camera.CFrame = Character.Head.CFrame
end)
EDIT: Comment #1, Keeping the Camera Scriptable
Remove the sections pertaining to "CameraType" but not at the beginning of the code, which changed the Type from "Custom" to "Scriptable". This can be seen below:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("CameraPart")
Camera.CameraType = "Scriptable"
Camera.CFrame = part.CFrame
game.ReplicatedStorage.Cam.OnClientEvent:Connect(function()
Camera.CFrame = workspace.CameraPart.CFrame
end)
game.ReplicatedStorage.Cam2.OnClientEvent:Connect(function()
Camera.CFrame = Character.Head.CFrame
end)
2 comments
You have to untie the Functions by writing :Disconnect()
@Feahren I dont uderstand