Hello, I wanna change a variable from a script in a diffrent script. I’ve already seen several ways on how to do this but they all seem “static”, that means if I copy paste the object / folder the scripts are in it will probably mess up.
I have a logic script that is supposed to increase a value based on it’s current status. I wanna change that status via diffrent buttons, each of them contain their own script to change that status. All of them are in a folder. My “main” script looks like this right now:
local displayNumber = script.Parent.ActualDisplay.SurfaceGui.TEXT
local displayExplanation = script.Parent.StatusExplaination.SurfaceGui.TEXT
local rodInjection = 100
local status = "Stay"
local function action()
while true do
while status == "Inject Rods" and rodInjection<100 do
rodInjection=rodInjection+1
displayNumber.Text=rodInjection
displayExplanation.Text = "Injecting Rod"
wait(1.2)
end
while status == "Eject Rods" and rodInjection>0 do
rodInjection=rodInjection-1
displayNumber.Text=rodInjection
displayExplanation.Text = "Ejecting Rod"
wait(1.2)
end
while status == "Stay" do
displayExplanation.Text = "Holding Position"
wait(1.2)
end
end
end
function changeStatus(newStatus) -- The Buttons call this and do de thing
status=newStatus
end
My goal is that if I duplicate the folder and give it a diffrent name (in this case a number) the scripts only work in their folders and don’t escape from it and mess everything up.
Any ideas on how I should do this? I already tought of just changing a label, however that seems inefficent.
use _G. to make global variables, they don’t need any object to work, but they are not replicated so you might consider another approach if you don’t want to make a way to replicate them.
But then I would have to rename the _G values to diffrent names for each script which is the thing I don’t want
I tried dymanically creating the variable _G names but that didn’t work either as it just says “.LogicScript:6: attempt to call a nil value”
local displayNumber = script.Parent.ActualDisplay.SurfaceGui.TEXT
local displayExplanation = script.Parent.StatusExplaination.SurfaceGui.TEXT
local rodInjection = 100
local statusName = "rod"..script.Parent.Name
_G.getfenv()[statusName] = "Stay"
print(_G.getfenv()[statusName])
function action()
while true do
while _G.getfenv()[statusName] == "Inject Rods" and rodInjection<100 do
rodInjection=rodInjection+1
displayNumber.Text=rodInjection
displayExplanation.Text = "Injecting Rod"
wait(1.2)
end
while _G.getfenv()[statusName] == "Eject Rods" and rodInjection>0 do
rodInjection=rodInjection-1
displayNumber.Text=rodInjection
displayExplanation.Text = "Ejecting Rod"
wait(1.2)
print("YES")
end
while _G.getfenv()[statusName] == "Stay" do
displayExplanation.Text = "Holding Position"
wait(1.2)
end
end
end
My first recommendation, is to just combined all the button connection into one script. So that they all can share their variables. This is probably the more ideal approach.
Second recommendation would to use NumberValue objects stored in ServerStorage and then use Instance:GetPropertyChangedSignal() to have scripts efficiently update the variables.
Thanks, I managed to solve the problem by connecting all buttons in one script, didn’t know you could do that, whoops! Thanks alot, also thanks @Cristagolem for trying to help, I appreciate it alot!
If anyone from the future finds this my code looks like this now:
local displayNumber = script.Parent.ActualDisplay.SurfaceGui.TEXT
local displayExplanation = script.Parent.StatusExplaination.SurfaceGui.TEXT
local rodInjection = 100
local status = "Stay"
local clickInject = script.Parent.Button_Inject.clickInject
local clickStay = script.Parent.Button_Stay.clickStay
local clickEject = script.Parent.Button_Eject.clickEject
local function action()
while true do
while status == "Inject Rod" and rodInjection<100 do
rodInjection=rodInjection+1
displayNumber.Text=rodInjection
displayExplanation.Text = "Injecting Rod"
if status == "Inject Rod" and rodInjection==100 then
status="Stay"
end
wait(1.2)
end
while status == "Eject Rod" and rodInjection>0 do
rodInjection=rodInjection-1
displayNumber.Text=rodInjection
displayExplanation.Text = "Ejecting Rod"
if status == "Inject Rod" and rodInjection==0 then
status="Stay"
end
wait(1.2)
end
while status == "Stay" do
displayExplanation.Text = "Holding Position"
wait(1.2)
end
wait(2)
end
end
local function onClickInject()
status="Inject Rod"
end
local function onClickStay()
status="Stay"
end
local function onClickEject()
status="Eject Rod"
end
clickInject.MouseClick:connect(onClickInject)
clickStay.MouseClick:connect(onClickStay)
clickEject.MouseClick:connect(onClickEject)
action()