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

How to make a variable become a part number?

What Im trying to do is make a variable (Lets call this variable N) that will go up as I click a gui button which is easy, but as that variable goes up I have a list of parts called Camera1, Camera2, Camera3, etc. Instead of making an if statement for every Camera I want every time the variable goes up, for example N = 2 I would want my charectors Camera.Cframe to switch to Camera2's Camera.Cframe. Thanks

local CurrentMap = 1


Next.MouseButton1Click:connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	
	if CurrentMap ~= 2 then
		CurrentMap = CurrentMap + 1
		if CurrentMap == 2 then
			Camera.CFrame = workspace.Camera2.CFrame
		end
	end
end)


Previous.MouseButton1Click:connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	
	if CurrentMap ~= 1 then
		CurrentMap = CurrentMap - 1
		if CurrentMap == 1 then
			Camera.CFrame = workspace.Camera1.CFrame
		end
	end
end)

2 answers

✓ Accepted answer

If you have a variable 'n' and want to access a child like Camera1, you can do:

  • workspace["Camera" .. n] - if you know the child exists
  • workspace:FindFirstChild("Camera" .. n) - if you aren't sure if the child exists

Some recommendations for your script:

  • Use a function to handle updating the camera (this way the code is only in one place)
  • Make sure to check for the limits -- ex, if the player goes to the previous camera when on Camera1, you don't want to try and access Camera0!

Example:

local CurrentMap = 1
local NumMaps = 3

function UpdateCamera()
    Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = workspace["Camera" .. CurrentMap].CFrame
end

Next.MouseButton1Click:Connect(function()
	CurrentMap = CurrentMap + 1
	if CurrentMap > NumMaps then
		CurrentMap = 1
	end
	UpdateCamera()
end)


Previous.MouseButton1Click:Connect(function()
	CurrentMap = CurrentMap - 1
	if CurrentMap < 1 then
		CurrentMap = NumMaps
	end
	UpdateCamera()
end)

4 comments

0

Awesome! thanks man.

0

I also have another question why would you put two dots between "Camera" .. CurrentMap, I don't really understand that.

0

That's the concatenation operator; it joins strings together, ex: print("Hello " .. "There") -- Hello There

0

alrighty man, thanks much help!

Here try this out tell me if it doesn't work for this what you would need to do is set up 3 parts named Camera1 Camera2 and Camera3 in workspace and put this script inside the button u want them to press.

local CurrentCameraValue = 0
local Camera = workspace.CurrentCamera

script.Parent.MouseButton1Click:Connect(function()
if CurrentCameraValue <= 2 then 
CurrentCameraValue = CurrentCameraValue + 1
Camera.CameraType = Enum.CameraType.Scriptable
print("Camera"..CurrentCameraValue)
Camera.CFrame = workspace:WaitForChild("Camera"..CurrentCameraValue).CFrame
else return
end
end)

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