Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Camera view button not working when more than 1 train is spawned?

Should be a final question for a while. I have a gui that can change the players camera while driving. However, when you spawn more of the train, it messes up the camera. It's alright if its one train, but if you spawn more of the same then it gets messy.

Here is the camera script:

local CameraViewButton = script.Parent
local Camera = game.Workspace.CurrentCamera
local CameraView = 1
local TrainSeat = script.Parent.Parent.Parent.Parent.Parent.C153.MachinistSeat
local Seat = game.Players.LocalPlayer.Character.Humanoid.SeatPart

function ChangeCamera(View)
	if View == 1 then
		game:GetService("RunService"):UnbindFromRenderStep("UpdateCamera")
		Camera.CameraType = "Custom"
		Camera.CameraSubject = TrainSeat
	elseif View == 2 then
		Camera.CameraType = "Watch"
		Camera.CameraSubject = TrainSeat.Parent
	elseif View == 3 then
		Camera.CameraType = "Scriptable"
		game:GetService("RunService"):BindToRenderStep("UpdateCamera", Enum.RenderPriority.Camera.Value - 1, function()
			Camera.CoordinateFrame = CFrame.new(Seat.Parent.Poles.Position) * CFrame.Angles(math.rad(0), math.rad(Seat.Parent.Poles.Orientation.Y + 30), math.rad(0))
		end)
	end
end

function ChangeCameraView()
	CameraView = CameraView + 1
	if CameraView == 4 then
		CameraView = 1
	end
	ChangeCamera(CameraView)
end

CameraViewButton.MouseButton1Click:connect(ChangeCameraView)
	

Regen:

local Engine = script.Parent.Parent["C153"]
local CloneTrain = Engine:clone()
Button = script.Parent.Parent.WCSpawn

function spawnTrain()
    if Button.BrickColor == BrickColor.new(104) then
    local Model = CloneTrain:Clone()--Clones the backup so the script doesn't break
    Model.Parent = game.Workspace --Puts the model into the game
    Model:MakeJoints() --Makes it so the model doesn't fall apart.
    Model.Bogies.SD_Bogie2:MakeJoints()
    Model.Bogies.SD_Bogie:MakeJoints()
    Button.BrickColor = BrickColor.new(26)
    wait(45)
    Button.BrickColor = BrickColor.new(104)
    end
end


script.Parent.ClickDetector.MouseClick:connect(spawnTrain)

Help appreciated.

Thanks for reading.

2 answers

✓ Accepted answer

You can add a variable that determines whether or not the camera can be changed.

local Occupied = false 

function ChangeCameraView()
	if not Occupied then -- if occupied is false then 
	    Occupied = true -- sets it to true so it can't be run again
	    CameraView = CameraView + 1
	    if CameraView == 4 then
	        CameraView = 1
	    end
	    ChangeCamera(CameraView)
	end
end

2 comments

0

I'd like it to be able to let everyone change their camera when they want.

0

Might as well make it an Acceptable Answer since no one else has replied.;

I think It's something to do with the TrainSeat variable. It says in the output it isn't a part of player.

This discussion is preserved as it was. New answers, comments, and votes are closed.