I am using these two scripts to make something right now but they don’t seem to work at the same time, only one can work if the other is disabled. Is there a way for me to combine them so that they can work at the same time?
I am using these two scripts to make something right now but they don’t seem to work at the same time, only one can work if the other is disabled. Is there a way for me to combine them so that they can work at the same time?
Just put the lines from the .RenderStepped function in the first script, in the .RenderStepped function in the second script, and obviously make sure to define the variables from the first script in the second script.
Sorry for the late response but now neither script works at all. I’m not getting any errors. I have redefined everything and added everything as how you said.
That’s weird. I will need to do some testing, but I am on mobile right now, so I should be able to help later today if no one does by then.
Alright, I did some testing. Try this code:
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local CorrectCameraType = Enum.CameraType.Custom
local MaxTilt = 10
RunService.RenderStepped:Connect(function()
if Camera.CameraType ~= Enum.CameraType.Custom then
Camera.CameraType = Enum.CameraType.Custom
end
Camera.CFrame = Camera.CFrame * CFrame.Angles(
math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -MaxTilt),
math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -MaxTilt),
0
)
local XOffset = math.random(-50, 50)/250
local YOffset = math.random(-50, 50)/250
local ZOffset = math.random(-50, 50)/250
if Character and Humanoid then
if Camera.CameraType ~= CorrectCameraType then
Camera.CameraType = CorrectCameraType
end
Humanoid.CameraOffset = Vector3.new(XOffset, YOffset, ZOffset)
end
end)
What I figured out is that setting the camera to Scriptable affects the Humanoid’s CameraOffset, so that’s why I made it so it changes the CameraType back to Custom before it tries changing the humanoid’s CameraOffset. I also made a few small changes.
This script doesn’t involve making the players camera the CFrame of the part. So I changed that.
But I found that the location of the code matters. When I put this script into StarterGUI, the camera just goes to the part and doesn’t shake. When I put it in StarterCharacterScripts, it doesn’t go to the part and instead goes to my player who’s camera violently spins around in circles at mach 10.
Where should I put the code and what is still wrong with this code that doesn’t make the two functions work?
RunService.RenderStepped
does not always Apply stuff in the exact same order, when you use RenderStepped
, it will be applied in a random order, if you want the code to be Applied under a set Priority, look into RunService:BindToRenderStepped
Sorry I messed up a bit of the code.
In the first if statement in the .RenderStepped function, change the Enum.CameraType.Custom to Enum.CameraType.Scriptable, in both lines.
For the location, it should be in StarterCharacterScripts or StarterPlayerScripts. It is currently not compatible with StarterPlayerScripts, but that is a pretty easy fix, you just need to change the way you get the character.
That just makes none of the script work.
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local CorrectCameraType = Enum.CameraType.Custom
local camPart = workspace.Lobby.MainMenuCamera
local MaxTilt = 10
RunService.RenderStepped:Connect(function()
if Camera.CameraType ~= Enum.CameraType.Scriptable then
Camera.CameraType = Enum.CameraType.Scriptable
end
camPart.CFrame = Camera.CFrame * CFrame.Angles(
math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -MaxTilt),
math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -MaxTilt),
0
)
local XOffset = math.random(-50, 50)/250
local YOffset = math.random(-50, 50)/250
local ZOffset = math.random(-50, 50)/250
if Character and Humanoid then
if Camera.CameraType ~= CorrectCameraType then
Camera.CameraType = CorrectCameraType
end
Humanoid.CameraOffset = Vector3.new(XOffset, YOffset, ZOffset)
end
end)
Now it just go back to only having the 2nd half of the script working.
Something I noticed when you are changing the CFrame this time, is that you swapped the cam part with the camera. That is probably the reason.
But that is the point, I’m trying to make the CurrentCamera the camPart.
Try this:
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local camPart = workspace.Lobby.MainMenuCamera
local MaxTilt = 10
if Camera.CameraType ~= Enum.CameraType.Scriptable then
Camera.CameraType = Enum.CameraType.Scriptable
end
RunService.RenderStepped:Connect(function()
local XOffset = math.random(-50, 50)/250
local YOffset = math.random(-50, 50)/250
local ZOffset = math.random(-50, 50)/250
Camera.CFrame = (camPart.CFrame * CFrame.new(Vector3.new(XOffset, YOffset, ZOffset)) * CFrame.Angles(
math.rad((((Mouse.Y - Mouse.ViewSizeY / 2) / Mouse.ViewSizeY)) * -MaxTilt),
math.rad((((Mouse.X - Mouse.ViewSizeX / 2) / Mouse.ViewSizeX)) * -MaxTilt),
0
))
end)
Thank you so much for your help with this, I could not have done it without you. I am still relatively new to scripting so you assisting me with this has been a good help in my development.
You are welcome! Good luck with your scripting journey.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.