Hi, my name is shieldmr3, I’m a scripter and a game developer.
What’s the issue I’m having? I have a script that makes the camera move as the mouse moves, it’s a main menu. Here’s the script:
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Character = Player.Character or Player.CharacterAdded:Wait()
local ScreenGui = Player.PlayerGui:WaitForChild("PlayOrNo")
local done = false
local TweenService = game:GetService("TweenService")
local camera = game.Workspace.CurrentCamera
local function tween(part1, part2, TimeToStop)
local tweenInfo = TweenInfo.new(
TimeToStop,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
0,
false,
0
)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part1.CFrame
local Tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
Tween:Play()
wait(TimeToStop)
print("Got here")
local originalCFrame = camera.CFrame
local ScaleFactor = 1000
game:GetService("RunService").RenderStepped:Connect(function()
local centreOfScreen = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
local mouseDistanceFromCentre = Vector3.new((-mouse.X - centreOfScreen.X) / ScaleFactor, (mouse.Y - centreOfScreen.Y) / ScaleFactor, 0)
camera.CFrame = originalCFrame * CFrame.new(originalCFrame.LookVector + mouseDistanceFromCentre)
end)
done = true
end
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(hit)
game.Workspace.cam:Destroy()
tween(game.Workspace.Test1, game.Workspace.Test2, 13)
while wait() do
if done then
ScreenGui.Enabled = true
end
break
end
end)
game.ReplicatedStorage.CamChange.OnClientEvent:Connect(function(plr)
ScreenGui.Enabled = false
camera.CFrame = Character.Head.CFrame
camera.CameraType = Enum.CameraType.Custom
print("Custom camera!")
end)
When the player presses the play button, it makes the camera type Custom but it stays there, and the camera doesn’t come back to the player. So how can I fix this?
Don’t have to send it, I found the issue, or so I think. Misreviewed the code and missed the render stepped you set up.
You have setup a RenderStepped function in the original tween function which is still running and is never disconnected. This is overriding the regular camera behavior. To fix this simply create a variable for the event and disconnect it in the CamChange remote function.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local TweenService = game:GetService("TweenService")
local mouse = player:GetMouse()
local screenGui = player.PlayerGui:WaitForChild("PlayOrNo")
local camera = game.Workspace.CurrentCamera
local cameraRenderEvent
local done = false
local function tween(part1, part2, TimeToStop)
local tweenInfo = TweenInfo.new(
TimeToStop,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
0,
false,
0
)
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = part1.CFrame
local Tween = TweenService:Create(camera, tweenInfo, {CFrame = part2.CFrame})
Tween:Play()
wait(TimeToStop)
local originalCFrame = camera.CFrame
local ScaleFactor = 1000
cameraRenderEvent = game:GetService("RunService").RenderStepped:Connect(function()
local centreOfScreen = Vector2.new(camera.ViewportSize.X / 2, camera.ViewportSize.Y / 2)
local mouseDistanceFromCentre = Vector3.new((-mouse.X - centreOfScreen.X) / ScaleFactor, (mouse.Y - centreOfScreen.Y) / ScaleFactor, 0)
camera.CFrame = originalCFrame * CFrame.new(originalCFrame.LookVector + mouseDistanceFromCentre)
end)
done = true
end
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(hit)
game.Workspace.cam:Destroy()
tween(game.Workspace.Test1, game.Workspace.Test2, 13)
while wait() do
if done then
screenGui.Enabled = true
end
break
end
end)
game.ReplicatedStorage.CamChange.OnClientEvent:Connect(function(plr)
screenGui.Enabled = false
if cameraRenderEvent then
cameraRenderEvent:Disconnect()
end
camera.CFrame = character.Head.CFrame
camera.CameraType = Enum.CameraType.Custom
print("Custom camera!")
end)