Can't look anywhere while camera following where model is facing?

can i see your WHOLE script?

(30 charrsss)

1 Like

@Instance0new The entire train platform script:

-- Localscript inside of StarterCharacterScripts

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local LastHit
local LastTrainCFrame

local Function
local Function2

local Whitelist = {workspace.Trains}

Function = RunService.Heartbeat:Connect(function()
    local RootPart = player.Character.HumanoidRootPart
    local Ignore = player.Character
    local ray = Ray.new(RootPart.CFrame.p, Vector3.new(0,-20,0))
    local Hit, Position, Normal, Material
    if LastHit then
        Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,{LastHit})
        if not Hit then
            Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
        end
    else
        Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
    end
    if Hit then
        local Train = Hit
        if not LastTrainCFrame then
            LastTrainCFrame = Train.CFrame
        end
        local TrainCF
        if Train ~= LastHit and LastHit then
            TrainCF = LastHit.CFrame
        else
            TrainCF = Train.CFrame
        end
        local Rel = TrainCF * LastTrainCFrame:inverse()
        LastTrainCFrame = Train.CFrame
        RootPart.CFrame = Rel * RootPart.CFrame
        LastHit = Train
        local TrainPrimary = workspace.Trains:FindFirstChildWhichIsA("Model")
        Camera.CFrame = TrainPrimary.CFrame * CFrame.new(0,5,0) * Camera.CFrame
        if Train ~= LastHit then
            LastTrainCFrame = Train.CFrame
        end
    else
        LastTrainCFrame = nil
                LastHit = nil
             Hit = nil
    end
    
    if not Function2 then
        Function2 = player.Character.Humanoid.Died:Connect(function()
            Function:Disconnect()
            Function2:Disconnect()
        end)
    end
end)
1 Like
Camera.CFrame = TrainPrimary.CFrame * CFrame.new(0,5,0) * Camera.CFrame:ToEulerAnglesXYZ()
1 Like

@Instance0new It returned an error message:
“invalid argument #2 (Vector3 expected, got number)”

Alright everybody! After messing with my train platform code, I FINALLY MANAGED TO FIX IT! WOHOOOO! @Instance0new

What I did is just guess which one will work. The variable “Rel” which is this:

local Rel = TrainCF * LastTrainCFrame:inverse()

I think I am assuming this will be the relative CFrame of the train, multiplied by the previous train CFrame with a function of :inverse().

Now what I did to the camera is this:

Camera.CFrame = Rel * Camera.CFrame

You can see the camera nicely following where a train car is facing and I can look around freely! No issues/problems at all. Fixed the insane bug that the camera did last time. Insanely spinning around. Now, its all finally fixed!

The full code:

-- Localscript inside of StarterCharacterScripts
	
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')
local Camera = workspace.CurrentCamera
local UserInputService = game:GetService("UserInputService")
	
local LastHit
local LastTrainCFrame
	
local Function
local Function2
	
local Whitelist = {workspace.Trains}
	
Function = RunService.Heartbeat:Connect(function()
	    local RootPart = player.Character.HumanoidRootPart
	    local Ignore = player.Character
	    local ray = Ray.new(RootPart.CFrame.p, Vector3.new(0,-30,0))
	    local Hit, Position, Normal, Material
	    if LastHit then
	        Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,{LastHit})
	        if not Hit then
	            Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
	        end
	    else
	        Hit, Position, Normal, Material = workspace:FindPartOnRayWithWhitelist(ray,Whitelist)
	    end
	    if Hit then
			local Train = Hit
			print(Train)
	
	        if not LastTrainCFrame then
	            LastTrainCFrame = Train.CFrame
	        end
	        local TrainCF
	        if Train ~= LastHit and LastHit then
	            TrainCF = LastHit.CFrame
	        else
	            TrainCF = Train.CFrame
	        end
	        local Rel = TrainCF * LastTrainCFrame:inverse()
	        LastTrainCFrame = Train.CFrame
	        RootPart.CFrame = Rel * RootPart.CFrame
			LastHit = Train
			Camera.CFrame = Rel * Camera.CFrame
	        if Train ~= LastHit then
	            LastTrainCFrame = Train.CFrame
			end		
	    else
	        LastTrainCFrame = nil
		    LastHit = nil
			Hit = nil
	    end
	    
	    if not Function2 then
	        Function2 = player.Character.Humanoid.Died:Connect(function()
	            Function:Disconnect()
	            Function2:Disconnect()
	    end)
	end
end)

I highly thank everyone for doing their best to help me! I appreciate the help and effort you did to help! Thanks a lot! :smiley:

2 Likes