Need help with random number generation

Hello developers! what I have here is a snippet of my code where essentially, I would like to create a new textlabel “popUp” in a random location on the gui, a typical pop-up gui counter as seen in classic clicker simulators.

Here is my code:

function generate(popUp)
	popUp = Instance.new("TextLabel")
	popUp.Parent = script.Parent.Parent
	popUp.Size = UDim2.new(0, 75, 0, 75)
	popUp.BackgroundTransparency = 1
	popUp.TextScaled = true
	popUp.Font = Enum.Font.AmaticSC
	popUp.Text = "+"..multiplier.."click"
	
	local uiCorner = Instance.new("UICorner") -- Creating UI corner for popUp
	uiCorner.CornerRadius.Scale = 1
	uiCorner.CornerRadius.Offset = 0
	uiCorner.Parent = popUp
	
	local uiStroke = Instance.new("UIStroke") -- Creating UI stroke for popUp
	uiStroke.LineJoinMode = Enum.LineJoinMode.Round
	uiStroke.Color = Color3.fromRGB(0, 0, 0)
	uiStroke.Thickness = 1
	uiStroke.Parent = popUp
	
	popUp.Visible = true -- Defining popUp tweens
	
	local randomX = math.random(1, 100)
	local randomY = math.random(1, 100)
	local xVal = randomX/100
	tonumber(xVal)
	
	popUp.Position = UDim2.new(xVal/100, 0, randomY/100, 0)
	
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.In) -- Tween popUp
	local tweenPop = TweenService:Create(popUp, tweenInfo, {Position = UDim2.new(xVal, 0, 0, -0.1)}, {TextTransparency = 1})
	tweenPop:Play()
	
	local tweenStrokeTrans = TweenService:Create(uiStroke, tweenInfo, {Transparency = 1}) -- Tween popUp.UIStroke
	tweenStrokeTrans:Play()
	
	task.wait(1.1)
	popUp:Destroy()
	
end

multiple is just a variable that is defaulted at 1

My issues are:

  1. The most important: I believe ‘xVal’ is a string (?), which is why I tried to convert it to a number since the arguments for UDim2.new has to be numbers. I am not sure if i am able to use variables to substitute numbers? As it wouldn’t work with how I wanted it to be if it wasn’t. What I want to achieve here is to only tween the Y-position of the newly generated ‘popUp’ Instance, and not the X-position altogether.

I am unsure if there are ways to go about this with or without using UDim2.new() to achieve the result of tweening only the y axis here. I tried using UDim() but that doesn’t seem right and did not work.

  1. My UICorner syntax might be wrong? I am also unsure of the syntax for UIStroke…If anyone knows anything about it or have the link to the documentation (I couldn’t find or I’m blind…), please help me to correct my code :-;

  2. The popUps generates and is stuck in the top left corner without tweening at all, it was working perfectly fine where it generates in a random location of the gui screen, and tweens up through the y axis only. I don’t remember what I did, but I tried to fix the syntax of the UIStroke and UICorner which kind of messed everything up…

It’s been a while since…again, but this time I am currently studying comp sci so I have more knowledge about coding in general now. Thanks for reading…I don’t know how to conclude this…

1 Like
  1. yes variables can substitute numbers. Almost every single one of my script do that.
    1.5. make a new intvalue, and put the value of the intvalue
  2. Any error messages? UI Stroke: UIStroke | Documentation - Roblox Creator Hub
  3. Well you could always try to revert to previous versions?
2 Likes

After some reviews of the documentations, I got it to work now as expected now, thanks for linking the documentations! Turns out I’ve just been missing a few lines of code due to me rushing through it as a speedrun

(Had to edit this post to this cause I deleted the other reply and did not know there was some restrictions to posting a reply with the same words, where I initially replied in this reply with follow-up questions of your reply)

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