Hello developers! essentially, I wanted to make a text button kind of “float around” the screen in a random direction through using tween service.
This is my first time attempting to use the ‘math.Random’ function and I don’t know how I should go about doing this…here is my hideous code…
local TweenService = game:GetService("TweenService")
local victim = script.Parent
local newPos = victim.Position
local startingPos = victim.Position
local posX = victim.Position.X
local posY = victim.Position.Y
local borderSize = victim.Parent.Border.Size
local randomPos = UDim2.fromScale(posX, posY) + math.random(1, 10) -- the line in question
local tweenInfo = TweenInfo.new(
2,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out
)
local weee = TweenService:Create(victim, tweenInfo, randomPos)
while true do
if newPos ~= startingPos then
task.wait(1)
victim.Position = startingPos
weee:Play()
victim.Position = newPos
end
end
I know there is something wrong about using the addition operator assigning to 2 values of posX and posY. I am very unsure of how I should write out the randomly generated numbers to plug it in as the new X or Y value. This is just a random exercise as I have not been doing much programming in a while, so I would gladly appreciate any help that keeps me back on track.
You cannot add UDim2s and numbers, it would be the same as doing something like 2 + true. You can, however, place your math.random() inside Udim2.fromScale:
local randomPos = UDim2.fromScale(posX + math.random(), posY + math.random())
(of course, remember to place your number range inside math.random).
If you instead wanted math.random() to add an offset, not a scale, you can do:
local randomPos = UDim2.new(posX, math.random(), posY, math.random())
Last of all, if you’re working with Scale then you probably want something that’s not an integer (since you usually don’t want scale values to go over 1): if you want to generate random decimal values, this is what you can do:
local RNG = Random.new()
local randomPos = UDim2.fromScale(posX + RNG:NextNumber(), posY + RNG:NextNumber())
(Once again, place max and minimum values inside the parentheses for :NextNumber()).
So I changed my approach and rewrote 3 different times but to no avail. I thought this was pretty straightforward…
local TweenService = game:GetService("TweenService")
local victim = script.Parent
local Position = victim.Position
local function rng()
local rgn = math.random(0.05, 0.477)
return rgn -- randomly generated numbers
end
local function rgd() -- randomly generated dimensions
local rgn = rng()
local rPos = UDim2.new(rgn, 0, rgn, 0)
local tweenInfo = TweenInfo.new(1.570795, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local function weee()
TweenService:Create(victim, tweenInfo, {Position = rPos})
end
while true do
task.wait(1)
weee()
end
end
I’m not sure if this is similar to what you meant, but I did use the first information to build this code.
Oh that make sense. I overlooked the part where the documentation stated that it returns an integer. However, it still didn’t work as intended (text button is still not moving at all)…I’m thinking of scraping this idea completely and just move on.
rPos and tweenInfo are inside the rgd function so you cannot access it from outside(tell me if i understood the code wrong).Also you are creating the tween but not playing it
It should be like this
local function Tween()
local rgn = rng()
local rPos = UDim2.new(rgn, 0, rgn, 0)
local tweenInfo = TweenInfo.new(1.570795, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local Tween = TweenService:Create(victim, tweenInfo, {Position = rPos})
Tween:Play()
end
Man I totally didn’t realise the tween wasn’t played within the function itself…Thanks a lot it works now (halfway there but I’ll figure it out for the rest on my own)