How do you go through a for pair in a certain order?


Issue

I’m currently making a custom custscene/camera manipulation system.

  • It skips all of the table sent, except the last two,
  • The coding is very messy,
  • It only works half the time,

Here’s a video showing the issue.

Goal

The goal is to do the following;

  • Send table data to each client and manipulate the camera properly from the beginning of the table, top to bottom, and only move to the next table once the first as completed.
  • Make the code efficient, and tidy.

Scripting

Here’s the file for testing: Example.rbxl (119.7 KB)

  • This is in a Script that runs off the client. image
Expand for the coding
--[[Created Functions]]
Functions = {
	["Change Camera Subject"] = {
		Description = "",
		Function = function(CameraType,CameraProperties,Info,Frames)
			local Camera = game.Workspace.CurrentCamera
			if string.lower(CameraType) == "hard camera" then --[[There's more than one argument, which is why this is here]]
				if not Frames then return end
				for NumPosition,Frame in pairs(Frames) do
					if not Frame then return end
					local bool = true
					if Frames[(NumPosition + 1)] ~= nil then
						--[Variables]
						local newCFrame;local tweenPosition
						Camera.CFrame = CFrame.new(Frame.Position) * CFrame.Angles(math.rad(Frame.Orientation.X), math.rad(Frame.Orientation.Y), math.rad(Frame.Orientation.Z)); --[[This will move the camera to the correct starting position.]]
						--Camera.CameraType = CameraProperties["Camera Type"];
						--Camera.FieldOfView = CameraProperties["FoV"];
						newCFrame = {CFrame = CFrame.new(Frames[(NumPosition + 1)].Position) * CFrame.Angles(math.rad(Frames[(NumPosition + 1)].Orientation.X), math.rad(Frames[(NumPosition + 1)].Orientation.Y), math.rad(Frames[(NumPosition + 1)].Orientation.Z))} --[[This is the CFrame value that the camera moves to.]]

						--[Function that moves the Camera Rig]
						if newCFrame ~= nil then
							tweenPosition = game:GetService("TweenService"):Create(Camera,TweenInfo.new(table.unpack(Info)),newCFrame)
							tweenPosition:Play()
							tweenPosition.Completed:Connect(function()
								bool = false
							end)
							repeat
								task.wait(.05)
							until not bool
						end
					end
				end
			end
			return
		end,
	}
}


if game.ReplicatedStorage:FindFirstChild("ReplicatedStorage_J2T") then
	game.ReplicatedStorage.ReplicatedStorage_J2T.Remotes.Events.ControlMain.OnClientEvent:Connect(function(call,arg1,arg2,arg3,arg4,arg5,arg6)
		for i,v in pairs (Functions) do
			if string.lower(i) == string.lower(call) then
				v.Function(arg1,arg2,arg3,arg4,arg5,arg6)
			end
		end
	end)
end

You probably wouldn’t be having this coding problem if your code was just a bit more organized. Try rewriting it knowing what you know now, but with organization in mind. Try to separate code bits into their own categories and keep your code DRY (Don’t repeat yourself), not wet (We enjoy typing).

1 Like

The issue is, I’m not sure whch for pairs to use, or what each do. Could you help me with that?

Try using guard clauses for your code, rather than nesting.

-- bad
if condition then
    -- code
end

-- good
if not condition then return end

-- code
1 Like

You don’t need to use pairs anymore, Roblox does it automatically. If you’re working with an array, it will use ipairs(), a dictionary, pairs().

Fix your coding, organize it, simplify it, then come back if you’re having the same issues. It’s 100 more times harder to debug code when it’s scattered all over the place.

1 Like

Alright, I’ve looked at this coding 100 times and counting and I have no idea what most to any of your responses truly mean.

Firstly;

Thank you for the coding example, I have updated the coding to match this idea. I however, know very little about guard clauses… could you please explain more to me of what these are and how these operate? Perhaps a place where I can learn this?

I do understand that a bunch of my coding can be hard to read, so I have gone ahead and fixed the coding to be easier to read. This, I believe, is what you recommended

Now, onto the actual point of this reply;

This makes absolutely no sense to me, could you please give me an example?

You say that they need to fix their coding; yet fail to tell me what the actual issue was. Telling someone to fix their coding does not tell them what is exactly wrong. I didn’t put any error from the output in my post because there was no problem that appeared in the output.

Please elaborate much more on this, so I can better understand your recommendations.

Try table.sort, the pairs loop will go through the table in the order that the table is currently in, using table.sort should fix your issue.

1 Like

Could you show me an example of how table.sort works? I’m trying to figure it out, and I can’t even get it to print an a or b value

table.sort(Frames,function(a,b)
	print(a.." | "..b)
end)

Here this is a basic example of how it could work.

local Table = {
	[1] = "fish",
	
	[3] = "turtle",
	
	[2] = "dog",
}



table.sort(Table,function(a,b)
	-- Since I want to compare the I values I can use table.find
	local FirstIndex = table.find(Table,a)
	local SecondIndex = table.find(Table,b)
	
	if FirstIndex < SecondIndex then
		return true
	else
		return false
	end
end)


print(Table) -- > {[1] = "fish",[2] = "dog",[3] = "turtle"}
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.