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

Change the value of a variable in 1 script with another script?

So I have a GUI with a local script that allows the GUI to toggle when the key is pressed.

local var = "k"

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
	if KeyPressed == var then
		if script.Parent.Main.Visible == false then
			script.Parent.Main.Visible = true
		else
			script.Parent.Main.Visible = false
		end
	end
end)

But my GUI also has a settings tab. The settings tab contains a textbox in which I would like to set the toggle key.

Code of the textbox:

TextBox.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
	if enterPressed then
		script.Parent.Parent.Parent.LocalScript:howwouldIaccessthevairiable
	else
		print("Player pressed", inputThatCausedFocusLost.KeyCode)
	end
end)

I would like to know how I can set the value of "var" to whatever the value of the textbox is. Yes I know I could just make the toggle script child of the textbox instead of a child of the ScreenGui but I have other needs where that wouldn't work.

Image:

https://bit.ly/397o5A1

1 answer

✓ Accepted answer

You cant, even with a module script. Even if there was a way to do that directly, its easier to just use a value. Put which value object you want inside a script, then you can change it with other scripts. Make sure when changing Values, you dont just locate to the value's name and try to change it, you have to change the .Value property of a value object for it to work properly. Or just access the value for the script itself to work same thing, Valuename.Value.

so in this script:

local var = "k"

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
    if KeyPressed == var then
        if script.Parent.Main.Visible == false then
            script.Parent.Main.Visible = true
        else
            script.Parent.Main.Visible = false
        end
    end
end)


Put a Value inside name anything, lets say name it "var".

ANd then to acess variable change tat value so the other script would be:

TextBox.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
    if enterPressed then
        script.Parent.Parent.Parent.LocalScript.var.Value = [value to set it to]
    else
        print("Player pressed", inputThatCausedFocusLost.KeyCode)
    end
end)

1 comment

0

this is assuming "script.Parent.Parent.Parent.LocalScript" is the script with the variable you want to change

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