Math.random doesnt work

im doing this script that creates a square, and the y axis of the square should be random. But it is spawning on the same spot everytime.

How do i fix it?

local function spawnSquare()
	local newSquare = square:Clone()
	local ypos = math.random(0.1,0.9)
	newSquare.Parent = startingFrame
	newSquare.Position = UDim2.new(0.5, 0, ypos, 0)
	newSquare.Rotation = 45
	newSquare.Visible = true
	
	local posTween = TS:Create(newSquare, TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.In),{Position = UDim2.new(-0.1, 0, newSquare.Position)})
	local sizeTween = TS:Create(newSquare.UIScale, TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.In),{Scale = 2.5})
	
	posTween:Play()
	sizeTween:Play()
	
	posTween.Completed:Connect(function()
		newSquare:Destroy()
	end)
end

wait(5)
repeat
	spawnSquare()
	wait(0.2)
until 1 == 2 --This is just to test

nuh uh uh, can’t use decimals.

Instead, just go between like 1 to 100 and divide it by 100 and do more stuff if you want.

with just math.random(0.1, 0.9), it will always output 0

1 Like

So how should i do it correctly?

something like this:
local ypos = math.random(1, 100) / 100

1 Like

Or alternatively if you want it to be able to have differences of 0.000000…1 or whatever, then you can also do

math.random() * 0.8 + 0.1

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