Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

SurfaceGUI won't add onto X axis?

bgui = script.Parent.Parent.Board.SurfaceGui.ScrollingFrame button = script.Parent

function onClicked(click) if bgui:FindFirstChild("ImageLabel") then local image1 = Instance.new("ImageLabel", bgui) image1.Position = UDim2.new(0,0,0,35) image1.Position = image1.Position + UDim2.new(0,120,0,0) image1.Size = UDim2.new(0,100,0,520) image1.Name = "" ..game.Players.LocalPlayer.Name image1.BackgroundColor3 = Color3.new(255, 255, 255) elseif bgui:FindFirstChild("ImageLabel") == nil then local image2 = Instance.new("ImageLabel", bgui) image2.Position = UDim2.new(0,10,0,35) image2.Size = UDim2.new(0,100,0,520) image2.Name = "" ..game.Players.LocalPlayer.Name image2.BackgroundColor3 = Color3.new(255, 255, 255) end end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

-- Basically trying to add onto the position everytime I click where it says "image1.Position = image1.Position + UDim2.new(0,120,0,0)" but it doesn't add the position on and it keeps at 120, please help. Someone said it might be something wrong with the other code.

1 comment

0

Please use code blocks. This hurts my eyes.

1 answer

It's because you're creating a new ImageLabel every time you click:

...
function onClicked(click)
    if bgui:FindFirstChild("ImageLabel") then
        local image1 = Instance.new("ImageLabel", bgui)
        image1.Position = UDim2.new(0,0,0,35)
...

To fix this, store image1 and image2 as variables outside the scope of the function, and set them only if they are undefined:

bgui = script.Parent.Parent.Board.SurfaceGui.ScrollingFrame
button = script.Parent

local image1
local image2

function onClicked(click)
    if bgui:FindFirstChild("ImageLabel") then
        if image1 == nil then
            image1 = Instance.new("ImageLabel", bgui)
            image1.Size = UDim2.new(0,100,0,520)
            image1.Name = "" ..game.Players.LocalPlayer.Name
            image1.BackgroundColor3 = Color3.new(255, 255, 255)
        end
        image1.Position = UDim2.new(0,0,0,35)
        image1.Position = image1.Position + UDim2.new(0,120,0,0)
    elseif bgui:FindFirstChild("ImageLabel") == nil then
        if image2 == nil then
            image2 = Instance.new("ImageLabel", bgui)
            image2.Position = UDim2.new(0,10,0,35)
            image2.Size = UDim2.new(0,100,0,520)
            image2.Name = "" ..game.Players.LocalPlayer.Name
            image2.BackgroundColor3 = Color3.new(255, 255, 255)
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

This discussion is preserved as it was. New answers, comments, and votes are closed.