Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my script to randomly move the text around not working?

Asked by
Taien 0
6 years ago
Edited 6 years ago

I'm trying to make a sci-fi game and I want my ScreenGuis to flicker occasionally (which I've mostly already achieved). I wanted to make it so that when this flicker briefly occurs on the screen, the positions of the TextLabel children of the script's Parent (the Frame) randomly change for a second, so the text kinda "glitches" to a different location on the screen, then moves back. Since all the positions of texts in my GUIs are based on Scale, not offset, I figured I could play with the offset to achieve this effect. I wrote the below script:

--init
math.randomseed(tick()) --set random seed

--functions
function OffsetText()
    for i, v in pairs(script.Parent:GetChildren()) do
        if v:IsA('TextLabel') then
            print("v = {" .. v.Position.X.Scale .. "," .. v.Position.X.Offset .. "},{" .. v.Position.Y.Scale .. "," .. v.Position.Y.Offset .. "}")
            local newPos = UDim2.new(UDim2.new(v.Position.X.Scale, math.random(600) - 300),UDim2.new(v.Position.Y.Scale, math.random(600) - 300))
            print("newPos = {" .. newPos.X.Scale .. "," .. newPos.X.Offset .. "},{" .. newPos.Y.Scale .. "," .. newPos.Y.Offset .. "}")
            v.Position = newPos
        end
    end
end

function ResetText()
    for i, v in pairs(script.Parent:GetChildren()) do
        if v:IsA('TextLabel') then
            v.Position = UDim2.new(UDim2.new(v.Position.X.Scale, 0),UDim2.new(v.Position.Y.Scale, 0))
        end
    end
end

--actual script
while true do
    local waitTime = math.random(300) / 60
    wait(waitTime)
    local choice = math.random(3)
    if choice == 0 then
        script.Parent.ScreenFlickerImage.ImageTransparency = 0
        OffsetText()
        wait(0.05)
        script.Parent.ScreenFlickerImage.ImageTransparency = 1
        ResetText()
    elseif choice == 1 then
        script.Parent.ScreenFlickerImage2.ImageTransparency = 0
        OffsetText()
        wait(0.05)
        script.Parent.ScreenFlickerImage2.ImageTransparency = 1
        ResetText()
    elseif choice == 2 then
        script.Parent.ScreenFlickerImage3.ImageTransparency = 0
        OffsetText()
        wait(0.05)
        script.Parent.ScreenFlickerImage3.ImageTransparency = 1
        ResetText()
    end
end

...but I'm not getting the desired effect. You can see I added some debug print()s in there, and I've used these to confirm that for some reason, creation of a new UDim2 for the position is resulting in all zero values, causing the text to end up at the top left of the screen and stay there after the first flicker.

Any ideas why this would be happening?

Here's some of the output from the debug print()s (there are two text objects in this display I'm using to write the script, so the first loop is the first four lines):

v = {0.5,0},{0,0}
newPos = {0,0},{0,0}
v = {0.5,0},{0.5,0}
newPos = {0,0},{0,0}
v = {0,0},{0,0}
newPos = {0,0},{0,0}
v = {0,0},{0,0}
newPos = {0,0},{0,0}

You can see that on the very first loop, it erases the value of the Scale of both coordinates.

Any help is greatly appreciated!

0
Put randomseed inside of the loop otherwise it will repeat the same stuff or use the new random function http://wiki.roblox.com/index.php?title=Random Vulkarin 581 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

Well this is all I see right off the bat:

local newPos = UDim2.new(UDim2.new(v.Position.X.Scale, math.random(600) - 300),UDim2.new(v.Position.Y.Scale, math.random(600) - 300))

UDim2 does not take UDim2 value arguments (that would be some weird recursion right lol). They take 4 numbers. Guess the "{0,0},{0,0}" confused you but thats only in explorer.

example fix:

local newPos = UDim2.new(v.Position.X.Scale, math.random(300), v.Position.Y.Scale, math.random(300))
Ad

Answer this question