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

Change a number in a string to a number value? Or store a udim2 value?

So basically I want to store the initial positions of GUIS. This is being done in a for child in pairs loop. However, since there is no Udim2 value to place in the child, I put in a stringvalue instead, and filtered out the brackets. However, to convert it back to a UDim2, I also need to have the number values in the strings (0,5,6,1) for example, to be numbers, and if i use tonumber() it returns nil as the commas cannot be numbers. Hopefully that makes sense and explains my problem well enough?

Just in case that made no sense whatsoever:

local player = game.Players.LocalPlayer
for i, child in pairs(player.PlayerGui.ScreenGui.Frame:GetChildren()) do
      local a = Instance.new("TextLabel",child)
      a.Text = tostring(child.Position)
end
wait(10)
script.Parent:TweenPosition(UDim2.new(0.366, 0,0.135, 0),"In",.5)
game.ReplicatedStorage.Sound:Play()
wait(1)
script.Parent.Sound:Play()
for i, child in pairs(player.PlayerGui.ScreenGui.Frame:GetChildren()) do
	local c = (string.gsub(child.TextLabel.Text,"{",""))
	local b = tonumber(string.gsub(c,"}",""))		
	print(b)
	child:TweenPosition(UDim2.new(b),"Out",.35)
end

1 answer

✓ Accepted answer

I would use a JSON string to hold this data which whould be simple due to the fact that a UDim2 prints as an array. ie {0, 0}, {0, 0} meaning that you convert it to an JSON array [[0,0],[0,0]]

Example:-

local defUDim2 = UDim2.new(0, 320, 0.5, 12)
print('UDim2 value is ', defUDim2)

-- formats string as a JSON
local function encode(data)
	return '['.. data:gsub('{', '['):gsub('}', ']') .. ']'
end

-- returns a table from the JSON
local function decode(data)
	return game:GetService('HttpService'):JSONDecode(data)
end

local encData = encode(tostring(defUDim2))
print('encoded UDim2 as JSON ', encData)

-- now a table
local decData = decode(encData)

local tmpUDim2 = UDim2.new(decData[1][1], decData[1][2], decData[2][1], decData[2][2])
print('Output UDim2 is ', tmpUDim2)

There are other ways this can be done this is just an example.

I hope this helps.

3 comments

0

The problem is im doing this in child in pairs loop so I need like a physical value outside of script to be stored you know?

0

Use a string value object

0

Works. It does not work if i store it in a variable first had to access it directly. You are a legend lmao, roblox really overcomplicated this just create a Udim value :(

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