Problem with math.random

I have a problem with math.random. When I use math.random in a function to set the position of GUI, the math.random stays as that math.random. Meaning the first time I fire the function it values at X. But if I fire it a second time, it still values as X. Below is part of the function.

script.Parent.MouseClick:Connect(function(player)
	print("clicked")
	script.Parent.Parent.Transparency = 1
	local Data1 = player:WaitForChild("Data1")
	local Data2 = player:WaitForChild("Data2")
	local Uncooked = Data1:WaitForChild("UncookedChiikens")
	local Multiplier = Data2:WaitForChild("Multiplier")
	local GUI = player.PlayerGui:WaitForChild("Add")
	local Label = GUI:WaitForChild("Label")
	
	local clone = Label:Clone()
	clone.Parent = GUI
	clone.Visible = true
	clone.Text = "+".. 1 * Multiplier.Value
	clone.Position = UDim2.new(math.random(0.100,0.500,0),math.random(0.100,0.500))
	script.Parent = script.Parent.Parent
	script.Parent.ClickDetector:Destroy()
	script.Parent.Deletion:Destroy()

I don’t think math.r is valid?
Instead, use this

clone.Position = UDim2.new(math.random(1), 0, math.random(1), 0)

Oops. forgot to change the script a bit. But the code you gave me still does not work.

What is your current code?

Also, it might be better for my code to h ave it as

clone.Position = UDim2.new(math.random(100)/100, 0, math.random(100)/100, 0)

Realised it would only return 0 or 1. now it can return 100 values

Not sure what you’re trying to do here, but UDim2.new requires 4 Numbers to set in chronological order:

  • X Scale
  • X Offset
  • Y Scale
  • Y Offset

You can just set clone.Position’s X Scale to math.random(), and since I don’t know what your actual Y Position is, I’ve just set it so only the X Scale will change along with making the Y Scale stationary

clone.Position = UDim2.new(math.random(), 0, clone.Position.Scale.Y, 0)

@SeargentAUS Not sure you know about this, but adding parameter to math.random(x) would generate a pseudo-random Integer, and not a Whole Number

In case the scaling is important here, we would want math.random() with no parameters here for this example as this returns a Whole Number between 0.000000000000000-1.000000000000000

Yeah, I realised that and corrected my mistake in post 4 ( 2 up ↑↑)

Read the code again. I have just edited the code so “math.r” does not exist.

Solved! Thanks for helping! I will just tweak your code so it can match what I’m trying to do.

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