Passing values between local scripts

Currently working on a tower defenses UI. When i click a unit, a UI should pop out displaying the units stats. My first idea was to have a user input local script detect when i click a unit so that it clones the GUI and puts it into my player GUI. Inside the UI there’s a local script that handles everything about the UI. Since the local script doesn’t know which unit it has to display, when cloning the UI I set object values to the unit stats (an int value instance with a bunch of attributes) and the unit itself. Inside the user input script where i cloned the UI, the values were fine, but inside the GUI handler local script, I’m not able to access the unit’s children and the stats attributes. Is there a way to pass an object argument into another local script WITHOUT USING BINDABLE EVENTS since the timing with cloning and displaying the stats on the screen will be broken if I have to wait for the bindable event.

Sorry if the explanation doesn’t make sense, please clarify if you need a better explanation and I’ll try explain again what I mean

This wouldn’t be an issue if you didn’t have a separate local script for each UI, you should probably rethink this design. One quick way you can fix it though is to store the stats as attributes inside the UI you just cloned so the script in it can access them

Are you saying I should just handle the UI in the same script that clones the UI? If so how would I update the UI If any stats change when the tower gets a buff/debuff? I’m only confused on that part. Thanks alot

Yes, the easiest way to update stats is to fire a remote when said buff is activated. You could also listen for .Changed event on your stat instances. Theres probably better ways to do this but these are some simple methods

there is no way you actually just suggested for him to control is entire client UI with one single script? You are insane

You can use a ModuleScript as a way to store shared variables between scripts of the same RunContext. Perhaps you can make a function in that module script to “pass an object argument” as you need to do

In theory, the method you proposed should have no problems. Are you sure all objects were loaded before the LocalScript that handles the UI uses them?

Okay turns out it was because I was setting the object values first before parenting it into the workspace (Player Gui) therefore the local script wasn’t able to access the children and attributes.

Basically just spent 3 days changing all the code while all it took was to just parent it first before setting the values.

1 Like

That wasnt the suggestion at all; I suggested for them to handle everything related to the upgrade UI within the same script for organization, not ALL UI.