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

I got some trouble here help pleas ? help this poor bacon guy finish his task ;(

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Mouse = player:GetMouse()

local BodyAngularVelocity = script.Parent:WaitForChild("BodyAngularVelocity")

local Patterns = { 
	Pattern1 = {
		Vector3.new(0, 0, 0),
		Vector3.new(0, 0, 0),
		0.0
	},
	
	Pattern2 = {
		Vector3.new(0, -10, 0),
		Vector3.new(0, 50, 0),
		0.25
	},
	
	Pattern3 = {
		Vector3.new(0, 25, 0),
		Vector3.new(0, 50, 0),
		0.5
	}
}

Mouse.Button1Down:Connect(function()
	for Key, Variables in pairs (Patterns) do
		print(Key) -- here is the print thing
		BodyAngularVelocity.AngularVelocity = Variables[1]
		BodyAngularVelocity.MaxTorque = Variables[2]
		
		wait(Variables[3])
	end
end)

However, it keeps printing

Pattern2
Pattern3
Pattern1

Why!?!? WHY ROBLOX

help me else ill have a mental breakdown

4 comments

0

help

0

pweas

0

I don't get it, so what is it supposed to print out? everything seems fine

0

it should print the patterns in order as I've written them but it starts from the second to the first pattern

1 answer

Dictionaries don't behave like Arrays. In it's largest difference, it doesn't have any uniform containment, as it's indices are completely random, there is no way for it to have any form of natural order. This is why when iterating over the key-value pairs, they are arbitrarily outputted.

To resolve this problem, you can form an array containing the desired references in their proper order:

local scatteredDictionary = {
    letter2 = {"a"}; letter3 = {"b"};
    letter4 = {"c"}; letter1 = {"d"}
}

local correctOrder = {"letter2", "letter3", "letter4", "letter1"}

local allAttributes = {}
for _,index in pairs(correctOrder) do
        table.insert(allAttributes, scatteredDictionary[index]))
end

print(table.concat(allAttributes, ", "))
--// -> "a, b, c, d"

2 comments

0

thank you sir

0

i tri it

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