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

How to continually Instance.new() a camera to a specific block, every .5 seconds?

He's some example code that I'm working on. However, please don't only pay attention to the code. If you have your own idea, please share it.

-- Either put this in a seat, if it can only be used in a seat, or do your own thing with it.

function waitForChild(parent, childName)
	while true do
		local child = parent:findFirstChild(childName)
		if child then
			return child
		end
		parent.ChildAdded:wait()
	end
end

-- If you find any problems with this script, please message me (Daviidi), and I will try to fix them. :-)

local camdb
local mycamera
local hint = Instance.new("Hint")
local selid = 0
local selected = false
local inuse = false
local currentcam = 1
local newcamera = Instance.new("Camera")
local keycon
showhintid = 0

function ResetCamera()
	if not mycamera then
		return
	end
	mycamera.Parent = workspace
	workspace.CurrentCamera = mycamera
	mycamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	mycamera.CameraType = "Custom"
end

function displayhints(cursel)
	hint.Parent = game.Players.LocalPlayer
	local chid = showhintid + 1
	showhintid = chid
	hint.Text = "Security Cameras ON"
	wait(4)
	if (showhintid ~= chid) then
		hint.Text = "Press Q to start viewing security cameras"
		wait(5)
	end
	if (not selected) or (selid ~= cursel) or (showhintid ~= chid) then
		hint.Parent = nil
	end
end

function SelectCamera(ID)
	currentcam = ID
	if not camdb then
		return
	end
	local thiscam = camdb:GetChildren()[ID]
	if not thiscam then
		return
	end
	local chid = showhintid + 1
	showhintid = chid
	AttachCamera(thiscam)
	hint.Text = "Now Viewing: ".. thiscam.Name.. ", Press Q and E to switch"
	hint.Parent = game.Players.LocalPlayer
end

function AttachCamera(securitycam)
	if not mycamera then
		return
	end
	newcamera.CameraType = 1
	newcamera.Focus = securitycam.CFrame * CFrame.new(Vector3.new(0,0,-0.35001))
	newcamera.CoordinateFrame = securitycam.CFrame * CFrame.new(0,0,-0.35)
	newcamera.Parent = workspace
	workspace.CurrentCamera = newcamera
end

function KeyPress(key)
	if (not inuse) or (not camdb) then
		return
	end
	if (#camdb:GetChildren() < 1) then
		return
	end
	key = string.upper(key)
	if key == "Q" then
		if currentcam == 1 then
			currentcam = #camdb:GetChildren()
		else
			currentcam = currentcam - 1
		end
	elseif key == "E" then
		if currentcam == #camdb:GetChildren() then
			currentcam = 1
		else
			currentcam = currentcam + 1
		end
	else
		return
	end
	SelectCamera(currentcam)
end

script.Parent.Selected:connect(function(mouse)
	mycamera = workspace.CurrentCamera
	selected = true
	local cursel = selid + 1
	selid = cursel
	if not inuse then
		if camdb then
			if #camdb:GetChildren() > 0 then
				inuse = true
				keycon = mouse.KeyDown:connect(KeyPress)
				SelectCamera(currentcam)
			else
				local chid = showhintid + 1
				showhintid = chid
				hint.Text = "Could not find any security cameras"
				hint.Parent = game.Players.LocalPlayer
				wait(4)
				if (showhintid == chid) then
					hint.Parent = nil
				end
			end
		else
			local chid = showhintid + 1
			showhintid = chid
			hint.Text = "Could not find any security cameras"
			hint.Parent = game.Players.LocalPlayer
			wait(4)
			if (showhintid == chid) then
				hint.Parent = nil
			end
		end
	end
end)

script.Parent.Deselected:connect(function()
	inuse = false
	selected = false
	showhintid = showhintid + 1
	hint.Parent = nil
	if keycon then
		keycon:disconnect()
		keycon = nil
	end
	ResetCamera()
end)

if script.Parent.MustBeInSeat.Value then
	coroutine.resume(coroutine.create(function()
		while true do
			if not game.Players.LocalPlayer.Character.Humanoid.Sit then
				break
			end
			wait()
		end
		inuse = false
		selected = false
		showhintid = showhintid + 1
		hint.Parent = nil
		if keycon then
			keycon:disconnect()
			keycon = nil
		end
		ResetCamera()
		script.Parent:Remove()
	end))
end

camdb = waitForChild(workspace, script.Parent.CameraGroup.Value)

3 comments

0

i would recommend just changing the camera position

0

OK, any suggestions/help how to do it?

0

Just message Daviidi :)

1 answer

You could Interpolate the camera as you insert a new part.

while true do
local part = Instance.new("Part",workspace) -- create a  part
Camera = workspace.CurrentCamera -- reference the players camera
Camera.CameraType = "Scriptable" -- make it "scriptable" or "editable"
Camera.CameraSubject = part -- set the subject  to the part
Camera:Interpolate(CFrame.new(part.Position + Vector3.new(-5, 5, 0), part.Position), part.CFrame, 0.5) -- move the camera nicely
wait(0.5) -- wait
part:Destroy() -- destroy it 
end

1 comment

0

both the interpolate function of the camera and the 2nd parameter of instance.new are deprecated

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