Wait variable only updates when loop is restarted

i am using a variable as my wait time in my script and if i change the variable while the loops is running the wait time will stay the same here is my script

local wa = game.Workspace.UniBeams2.UI.Pages.Page1.Speed.Intensity.Value.Value * -0.035 + 0.5

while wait() do
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do

if v.Name == “U1”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U2”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U3”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U4”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U5”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U6”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U7”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U8”
then
v.LightingController.Pan:Invoke(-45)
end
end

wait(wa)

for i,v in pairs (game.Workspace.Ulights:GetChildren())

do
if v.Name == “U1”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U2”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U3”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U4”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U5”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U6”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U7”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U8”
then
v.LightingController.Tilt:Invoke(45)
end
end

wait(wa)

for i,v in pairs (game.Workspace.Ulights:GetChildren())

do
if v.Name == “U1”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U2”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U3”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U4”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U5”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U6”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U7”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U8”
then
v.LightingController.Pan:Invoke(45)
end
end

wait(wa)

for i,v in pairs (game.Workspace.Ulights:GetChildren())

do
if v.Name == “U1”
then
v.LightingController.Tilt:Invoke(135)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U2”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U3”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U4”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U5”
then
v.LightingController.Pan:Invoke(-45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U6”
then
v.LightingController.Tilt:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U7”
then
v.LightingController.Pan:Invoke(45)
end
end
for i,v in pairs (game.Workspace.Ulights:GetChildren())
do
if v.Name == “U8”
then
v.LightingController.Tilt:Invoke(135)
end
end

wait(wa)

end

That’s a suprising amount of lines of code I suggest using three backticks ``` to format it in between a sandwich. Anyways to update the wait value wa we need to use events to detect when the value changes like so:

local objectStoringTheVariable = game.Workspace.UniBeams2.UI.Pages.Page1.Speed.Intensity.Value

objectStoringTheVariable:GetPropertyChangedSignal("Value"):Connect(function(newValue)
wa = newValue* -0.035 + 0.5
end)

You can add this line of code after local wa is declared and before the while wait() in order to connect to the event first.

Edit: Also I suggest looking into dictionaries you can do a lot of stuff to shorten the if statements as well and also string concentation.

local lightsInvokeAmount = {
["U1"] = 45;
["U2"] = 180;
}

for _,Ulight in pairs (game.Workspace.Ulights:GetChildren()) do

	for i = 1, 8 do
			--the .. will concentate U and the number i so you can get
		--U1, U2, U3,...., U8
		--This goes into the dictionary and you can get the invoke amount you want
		Ulight.LightingController.Tilt:Invoke(lightsInvokeAmount["U"..i])

	end
end
1 Like

im still having the same issue it says this in the out put

Workspace.UniBeams1.UI.Pages.Page1.Screen.MovmentLoops.TextButton.mainloop:5: attempt to perform arithmetic (mul) on nil and number - Server - mainloop:5

It wont even update at all should the bit where i set the vale be set to 0.15 or should i put the old value

Looking at the errors it seems like it’s an issue with server-client since the script is a server script. And you are using a UI which is client sided and by default doesn’t replicate to the server automatically and so the value for the .Value will be nil causing the error I believe.

You will need to send a remote event in order to send this wa value from the GUI to the server to change the value of the wa value.

1 Like

what scripts would i need to add to make this work?

As @dthecoolest said you should consider converting to a system like this:

local wa = game.Workspace.UniBeams2.UI.Pages.Page1.Speed.Intensity.Value.Value * -0.035 + 0.5

local LightValues = {
	["U1"] = {45, 45, -45, 135},
	["U2"] = {135, 45, 45, -45},
	["U3"] = {-45, 135, 45, 45},
	["U4"] = {45, -45, 135, 45},
	["U5"] = {45, 45, 135, -45},
	["U6"] = {45, 135, -45, 45},
	["U7"] = {135, -45, 45, 45},
	["U8"] = {-45, 45, 45, 135}
}

local LoopIteration = 1

while wait() do
	for i,v in pairs (game.Workspace.Ulights:GetChildren()) do
		
		local ValueToSet = LightValues[v.Name][LoopIteration]
		
		v.LightingController.Pan:Invoke(ValueToSet)
		
	end
	
	if LoopIteration < 4 then
		LoopIteration += 1
	else
		LoopIteration = 1
	end
	
	wait(wa)

end
2 Likes

you forgot about tilt tho so wont it not work?

i did this but it still didnt help

Ah yeah, missed that. My bad. Here’s a fixed version to neaten up the code with Pan and Tilt accounted for.

local wa = game.Workspace.UniBeams2.UI.Pages.Page1.Speed.Intensity.Value.Value * -0.035 + 0.5

local LightValues = {
	["U1"] = {
		["Value"] = {45, 45, -45, 135},
		["Axis"] = {"Pan", "Tilt", "Pan", "Tilt"}
	},
	["U2"] = {
		["Value"] = {135, 45, 45, -45},
		["Axis"] = {"Tilt", "Pan", "Tilt", "Pan"}
	},
	["U3"] = {
		["Value"] = {-45, 135, 45, 45},
		["Axis"] = {"Pan", "Tilt", "Pan", "Tilt"}
	},
	["U4"] = {
		["Value"] = {45, -45, 135, 45},
		["Axis"] = {"Tilt", "Pan", "Tilt", "Pan"}
	},
	["U5"] = {
		["Value"] = {45, 45, 135, -45},
		["Axis"] = {"Tilt", "Pan", "Tilt", "Pan"}
	},
	["U6"] = {
		["Value"] = {45, 135, -45, 45},
		["Axis"] = {"Pan", "Tilt", "Pan", "Tilt"}
	},
	["U7"] = {
		["Value"] = {135, -45, 45, 45},
		["Axis"] = {"Tilt", "Pan", "Tilt", "Pan"}
	},
	["U8"] = {
		["Value"] = {-45, 45, 45, 135},
		["Axis"] = {"Pan", "Tilt", "Pan", "Tilt"}
	}
}

local LoopIteration = 1

while wait() do
	for i,v in pairs (game.Workspace.Ulights:GetChildren()) do
		
		local ValueToSet = LightValues[v.Name]["Value"][LoopIteration]
		
		if LightValues[v.Name]["Axis"][LoopIteration] == "Pan" then
			v.LightingController.Pan:Invoke(ValueToSet)
		else
			v.LightingController.Tilt:Invoke(ValueToSet)
		end
	end
	
	if LoopIteration < 4 then
		LoopIteration += 1
	else
		LoopIteration = 1
	end
	
	wait(wa)

end

Also how are you wanting to manage the ‘wa’ value?

If you want to do it from the client like @dthecoolest suggested then you will need a local script to fire a remote event to the server to tell it to change the value.

All depends on how you want to implement your system and how you want it to function.

1 Like

i just used this script and it fixed it
local wa = 0.25

local objectStoringTheVariable = game.Workspace.UniBeams2.UI.Pages.Page1.Speed.Intensity.Value

objectStoringTheVariable.Changed:Connect(function()

wa = objectStoringTheVariable.Value * -0.025 + 0.4

end)

Ah good.

Good luck with your project :slightly_smiling_face:

1 Like

Thank you for your help and i will modify my script to make the shorter

1 Like