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

Make variables(values) shared, but only between SOME scripts?

I have multiple LocalScripts that are inside a player model, but what I have intended won't work unless there's a way to make thouse scripts' variables(values) available through all of them, but not through anything else. Also I want thouse scripts to still function properly even if i clone them to different players on the same game, same server.

For example, I want to be able to do something like this:

Script1: local x = 1

Script2: local x = script.Parent.Script1.variable(x) print(x)

Output: Script2: 1

Of course there is a reason why I can't simply make the variable inside the same script. It's because the variable will be modified by a loop and I already have a loop inside the script in which I would want to have that variable(value).

I've heard of a method that would make the variable available to all scripts, but if it is cloned, then there'll be just a mess of multiple same name variables with different values.

1 comment

0

Why not put the variable inside a StringValue which all the local script can access?

1 answer

✓ Accepted answer

This is usually a bad idea because it's very confusing and easy to mess up.

ROBLOX gives you controlled ways for scripts to communicate with each other.

ModuleScripts let you define a script that "exports" an interface to different scripts. The object is shared among all server scripts / all localscripts on a given client, which means you can share local variables however you want, usually behind functions.

BindableFunctions are another way for scripts to talk to each other using functions.

You could also use plain old IntValue, ObjectValue, StringValue, etc. objects, but these are only the right way to organize your code from time-to-time.


If you explain specifically the kind of information you want to share, I could suggest a manageable way to set up your scripts.

2 comments

0

I'm already giving up on my intention, because it seems that no matter what I try to do Roblox will always find a way to break it. I was trying to do things that seem to be too complicated for Roblox to handle thus I shall start a much simpler project. I haven't done any research on modulescripts or bindable functions, but it might be the answer for someone else looking to solve this problem.

0

If you're still wondering what I was trying to do then it was creating somewhat of a realistic vehicle physics, such as, having rpm go up or down depending on player's inputs or accelerating slower or faster depending on the gear that the player has choosen. In th end all of theese things require loops to update their values and, so I wanted them to be split amongst many scripts, but it no longer

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