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

Is it possible to store locations on a table? If not where do I store them?

I'm working on a little effect with guis, but I have a problem with getting locations from a table. Here's a slice of the code:

local Positions = {0.251,0,-0.12,0;-0.062,0,-0.134,0;-0.069,0,0.116,0;0.162,0,0.173,0;0.32,0,0.079,0}--List of positions
local BannedPlayers = game.Workspace:WaitForChild("AdminGuiCommands"):WaitForChild("BannedPlayers"):GetChildren() --Just a list
local points = 0
for i = 1, 5 do
	local points = points + 1
	local clone = Instance.new("ImageLabel",script.Parent.Parent.Parent)
	local cloneplayer = BannedPlayers[i]
	--Some configurations, everything smooth here.
	clone.Position = UDim2.new(Positions[points])

	print(Positions[points]) --Prints "0.251,0,-0.12,0(x5)" which is the first position in the table
	wait(.2) 
end
points = 0

If you look at the code, you can clearly see that I'm trying to create 5 Image Labels and set each position to the corresponding position saved on the table, but that's just not happening.

Whenever I try to test, the images are created but they don't move. I tried to turn them into strings in the table and then make them tonumber() but that also never worked.

There's also no error displayed in the output, only the print 0.251,0,-0.12,0(x5). I don't know what to do at this point, any kind of help would be really appreciated!

2 answers

✓ Accepted answer

Hey wilson,

Just save the positions as UDim2 values in a table. Something like this:

positions = {
	UDim2.new(0.251,0,-0.12,0),
	UDim2.new(-0.062,0,-0.134,0),
	UDim2.new(-0.069,0,0.116,0),
	UDim2.new(0.162,0,0.173,0),
	UDim2.new(0.32,0,0.079,0)
}

for i = 1, 5 do
	ui = Instance.new("ImageLabel")
	ui.Position = positions[i]
end

####This code is just an example so it will not work on your game, but it's to show you the basic principle of what I am trying to teach here. So, just store UDim2 values inside of the table and then use those.

##I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

KingLoneCat

2 comments

0

Thank you very much for the information! Have a nice day.

0

No problem, glad to help.

I am not sure why you are using , and ; in the table they do the same thing in terms of separating each value.

You are overcomplicating this code somewhat as you can just store the Udim2 in the table then index them when needed.

Passing a table to Udim2 is not a valid constructor.

Lastly you should not really be using the parent argument for Instance.new follow the process, create, setup then parent to the game. This will help reduce the amount of changes needed.

Example:-

local UDim2List = {
	UDim2.new(0.251,0,-0.12,0),
	Udim2.new(-0.062,0,-0.134,0),
	Udim2.new(-0.069,0,0.116,0),
	Udim2.new(0.162,0,0.173,0),
	Udim2.new(0.32,0,0.079,0)
}
local par = script.Parent.Parent.Parent
for i=1, #UDim2List do
	local lable = Instance.new("ImageLabel")
	lable.Position = UDim2List[i] -- index the udim2 in the list
	lable.Parent = par
	wait(0.2)
end

Other

You are making a new variable in your loop local points = points + 1 which will mean that print(Positions[points]) is one each time. Remove the local as you have defined the variable in the code above.

local points = 0
for i = 1, 5 do
	local points  = points  + 1 -- this is a local variable in this scope
	print(points) -- prints 1
end
for i = 1, 5 do
	points  = points  + 1 -- update the points variable defined above
	print(points) -- prints 1~5
end

Hope this helps

3 comments

0

I will apply the changes you suggested me to, thank you very much. Unfortunately I cannot accept 2 answers. I'm sorry. But have a nice day!

0

Its ok as long as it helped.

0

The changes made the code more compact and optimized, thank you very much.

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