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

Is there a way to change things inside a script from another script ?

Asked by 7 years ago

Like maybe changing something from true to false inside one script with a script ?

2 answers

Log in to vote
3
Answered by 7 years ago

Hey 1Messi3903,

You can easily change values in one script from another script using Bindable/Remote Functions/Events. I will give you an example of changing them using Bindable Events/Functions, the Remote Functions/Events shouldn't be that hard to figure out after that. Remember that, Remote Functions/Events communicate from client-server or vice versa so, only use those when you're sharing information between local and server scripts. Otherwise, I'd just use Bindable Events/Functions. Mostly to mutate information I use Bindable Functions because, Bindable Functions can return information to the script as well, and I like to use Events for more practical uses like the main operations. Anyways, I'll show you an example of how you can use both, below:

---- Bindable Events ----
-- Script #1 --

local event = script.Parent:WaitForChild("Event") -- The variable for the Bindable Event.

event:Fire("ChangeVariableToSomething") -- Using the BindableEvent's :Fire() function to send the string that the variable will be changed to.

-- Script #2 --

local event = script.Parent:WaitForChild("Event") -- Variable for the event;
local value; -- The value that will be changed.

event.Event:Connect(function(val) -- Anonymous function connecting the BindableEvent's Event(which is triggered by :Fire()) to the function.
    value = val; -- Setting value to the string that is sent. ~ Doesn't have to be a string. It can be any value.
    print(value); -- Printing the value.
end) -- end for the anonymous function.
---- Bindable Events ----

Alright, the way I did it, I had to disable the first script and re-enable it every time I wanted it to print that value because, the game wasn't aware that the Event was connected at the beginning. So if you use this script, make sure you disable and enable the script. Under normal circumstances, you won't need to be doing this because you will most likely fire the event at a certain time in the game however, for my case I didn't want to get into any of the other things, so I just decided to fire it as soon as the script enabled.

Here is the screenshot of the hierarchy to maybe get a better understanding of this.

Now, let's move on to Bindable Functions

---- Bindable Functions ----
-- Script #1 --
local func = script.Parent:WaitForChild("Function"); -- Variable for the Function

func:Invoke("ChangeItToThisString"); -- Invoking the function to make it change the value.

-- Script #2 --
local func = script.Parent:WaitForChild("Function"); -- Variable for the function
local value; -- Variable for the value, declared as nil atm.

function change_value(val) -- Function that will change the value with the parameter of the value that it will be changed to.
    value = val; -- Setting value to the parameter.
    print(value); -- Printing value.
end -- end for the function.

func.OnInvoke = change_value; -- Detects when :Invoke() is fired on this Function, if it is fired then it connects it to this function called 'change_value'
---- BIndable Functions ----

The hierarchy with this is the same as the Bindable Events except for there being a BIndable Function instead of a BIndable Event. Also, same thing here is that you have to re-enable the first script in order for this whole thing to take place. You only need to do that because my first script is being enabled since the beginning. You most likely won't needed to re-enable a script to make this work in the future because, yours will be a certain instance through operational time. Mine was right at the beginning so, studio wasn't aware that the bindable function's OnInvoke event was being used.

Well, I hope you have a great day/night and that I was of some assistance to your cause.

~~ KingLoneCat

Ad
Log in to vote
0
Answered by 7 years ago

Well you can't have a script access another script because roblox stopped this method because of hackers hacking into the studio and stealing scripts

You could have an object and have another script change it

Here is script one:

local myInt = Instance.new("IntValue",game.Workspace)
myInt.Name = "myInt"
myInt.Value = 10
print(myInt)

Here is the second script:

local Int = game.Workspace.myInt
Int.Value = 15
print(Int.Value)

You will see in the output that it will print diffrent things Well this is an example of changing an object value, name etc.

0
But, if you where to make a plugin you could still use the Source property. For example, workspace.Script.Source . PyccknnXakep 1225 — 7y
0
Obviously it's not allowed in a regular script though. PyccknnXakep 1225 — 7y
0
Oh well I am not that familiar with plugins so I can't really help you at that saSlol2436 716 — 7y

Answer this question