How do I add a variable into a line of code?

Hello, I am new to roblox scripting and I need help.

local Gui2 = script.Parent.Parent.Value.Value

script.Parent.ClickDetector.MouseClick:Connect(function (player)
print(“worked”)
if player.PlayerGui.Gui2.Enabled == true then
player.PlayerGui.Gui2.Enabled = false
else
player.PlayerGui.Gui2.Enabled = true
end

end)

In the code above, I am trying to make it so I can type a name into the Intvalue, so I dont have to type Gui and then the number everytime. Whats happening is I dont know how to implement the variable above into the line of code. Im not sure if that makes since, but if anyone can help it would make my day.

1 Like

local v = player.PlayerGui.Gui2??
and you can use it like v.Enabled = true

Use a code block please

Like so

But I dont really get what you are saying

Variables are like : a = game.Workspace.part


this and this

its not the same?

1 Like

that doesnt fix the variable needing to be implemented into the script, Im using Gui2 as a variable for the name of the gui I want to be opened.

no one is gotted what exactly you need, respell

local Guiname = script.Parent.Parent.Value.Value

script.Parent.ClickDetector.MouseClick:Connect(function (player)
	print("worked")
	local v = player.PlayerGui.Guiname
	if v.Enabled == true then
		v.Enabled = false
	else
		v.Enabled = true
	end

end)

I re-worded the code Im not sure if this helps.

OH, i think i got what you mean?

script.Parent.ClickDetector.MouseClick:Connect(function (player)
    print(“worked”)
    local Gui2 = player.PlayerGui.Gui2

    if Gui2.Enabled == true then
        Gui2.Enabled = false
    else
        Gui2.Enabled = true
    end

end)

this is how we use local

or you need like search for gui that named same as the value?
if so, then

local Guiname = script.Parent.Parent.Value.Value

script.Parent.ClickDetector.MouseClick:Connect(function (player)
	print("worked")
	local v = player.PlayerGui[Guiname] -- this
	if v.Enabled == true then
		v.Enabled = false
	else
		v.Enabled = true
	end

end)
local Guiname = script.Parent.Parent.Value.Value

script.Parent.ClickDetector.MouseClick:Connect(function (player)
	print("worked")
	local v = player.PlayerGui:FindFirstChild(Guiname) -- this
    if v then -- if exist
	    if v.Enabled == true then
		   v.Enabled = false
	    else
		   v.Enabled = true
	    end
    end
end)

to evade errors, use this

Yes it actually worked, thank you! Before I was trying to use " … " instead of braces, and I couldnt figure out what was happening.

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