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

Using a Variable that was defined in a function?

Asked by 6 years ago
Edited 6 years ago

Hi, I'm having trouble since I would like to use a variable that I have defined inside a function that I called. Here is an example from my code:

function button()
    local startbutton = Instance.new("ImageButton")
    startbutton.Parent = gui
    startbutton.Name = "StartButton"
    startbutton.Size = UDim2.new(0, 100, 0, 28)
    startbutton.Position = UDim2.new(0, 5, 0, 570)
end

    startbutton.Mousebutton1Click:connect(newfunction)

Now, I can't use the variable startbutton outside of the function, which makes sense since the function is where I defined it. Is there a way to be able to use this variable again? Or are there any workarounds? On a side note, I've already tried using WaitForChild to wait for the new Image Button to appear, and it doesn't seem to be working as it stops the script and nothing happens.

0
The "Local" could be the problem make it a regular variable: startbutton instead of local startbutton OBenjOne 190 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago
local startbutton

function button()
    startbutton = Instance.new("ImageButton")
    startbutton.Parent = gui
    startbutton.Name = "StartButton"
    startbutton.Size = UDim2.new(0, 100, 0, 28)
    startbutton.Position = UDim2.new(0, 5, 0, 570)
end
button()
startbutton.Mousebutton1Click:connect(newfunction)

now the value of the variable startbutton is assigned witht he function but can be used outside it

0
Thanks! This is what I was looking for! MrBlockyhead 84 — 6y
Ad

Answer this question