Hello, So lately I have been working on a game and I am now working on a section for it.
It will spawn 3 random symbols with a random position inside a frame.
Here is the script I got so far.
for i, z in pairs(SymbolFolder:GetChildren()) do
local X = MainBattleParent.Position.X + math.random(-MainBattleParent.Size.X/2,MainBattleParent.Size.X/2)
local Y = MainBattleParent.Position.Y + math.random(-MainBattleParent.Size.Y/2,MainBattleParent.Size.Y/2)
z.Position = Vector3.new(X,Y,0)
z.Visible = true
end
When I test the game I get this error.
attempt to perform arithmetic (div) on UDim and number
When defining ‘X’ or ‘Y’ as a property of a size or position, you are defining the UDim (scale,offset). Depending on if you are using scale or offset, add that to X and Y. e.g (Size.X.Scale). also, you are setting the position of a UI with a Vector3 value, it should be UDim2.new().
It did something but I am still getting the same error.
Here is the script I got now.
for i, z in pairs(SymbolFolder:GetChildren()) do
local X = MainBattleParent.Position.X.Offset + math.random(-MainBattleParent.Size.X/2,MainBattleParent.Size.X/2)
local Y = MainBattleParent.Position.Y.Offset + math.random(-MainBattleParent.Size.Y/2,MainBattleParent.Size.Y/2)
z.Position = UDim2.fromOffset(X,Y)
z.Visible = true
end
This is because of Offset. Offset is the position on your screen in pixels. If you wanted it inside the frame, you can consider using math.clamp() with the AbosluteSize or use Scale.
for i, z in pairs(SymbolFolder:GetChildren()) do
local X = math.random(-MainBattleParent.Size.X/0,MainBattleParent.Size.X/1)
local Y = math.random(-MainBattleParent.Size.Y/0,MainBattleParent.Size.Y/1)
z.Position = UDim2.fromScale(X,Y)
z.Visible = true
end
Instead of using offset, just use scale, and use random.new() as the scale values.
local random1 = random.new()
local random2 = random.new()
for i, z in pairs(SymbolFolder:GetChildren()) do
random1:NextNumber()
random2:NextNumber()
z.Position = Udim2.new(random1,0,random2,0)
z.Visible = true
end