I know it’s something simple but someone please help me understand. Why does the server script below run continuously regardless of what the transparency of the object is?
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
local db = true
--\\ Functions
while part.Transparency ~= 1 and db == true do
db = false
part.BrickColor = BrickColor.random()
task.wait(1)
db = true
end
This is because you are not checking if the part property as been updated. You should change the code to the following:
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
local db = true
--\\ Functions
part.Changed:Connect(function()
while part.Transparency ~= 1 and db == true do
db = false
part.BrickColor = BrickColor.random()
task.wait(1)
db = true
end
end)
Tell me if it doesn’t work, as I haven’t tested it.
You could try to do something like this, I haven’t tested it yet thoooo…
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
local db = true
--\\ Functions
while task.wait(1) do
if part.Transparency ~= 1 and db == true then
db = false
part.BrickColor = BrickColor.random()
db = true
end
end```
You could try to do something like this, I haven’t tested it yet thoooo…
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
local db = true
--\\ Functions
while task.wait(1) do
if part.Transparency ~= 1 and db == true then
db = false
part.BrickColor = BrickColor.random()
db = true
end
end```
The script runs the while-loop if the part isn’t fully transparent. That is if the transparency is .5, it will always run. Also, I believe you don’t need the “db” variable since it doesn’t really do anything anyways (from what I can see).
This didn’t work. Firing up the test server with the part Transparency at 0, the color stayed solid. That makes sense because the while loop is contained in a function that is only triggered from a change event.
I even tried to trigger the change event by changing the transparency to 0.5 while the server was running and that threw an error and locked up the server lol.
Error: Maximum event re-entrancy depth exceeded for Instance.Changed (x5000)
I got rid of the db since it wasn’t doing anything. Although, I think you may misunderstand my question. I get what the script does because I wrote it. What I’m saying is that when I run it, the part changes colors every second, regardless of what the transparency is. If I set the transparency to 1, it still changes colors and doesn’t stop. I thought I might need to include an if then statement with a break command, maybe? Although I thought you didn’t have to do that with a while loop.
Update: I changed the script to include an IF statement to break the loop if the Transparency is ever 1. That worked in stopping the while loop. However, when I changed the transparency back to 0 again, the while loop never re-engaged.
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
--\\ Functions
while part.Transparency ~= 1 do
part.BrickColor = BrickColor.random()
task.wait(1)
if part.Transparency == 1 then
break
end
end
In order to reconnect a loop, you’ll have to establish some sort of function so that you’re able to properly call the Part whenever it changes Transparency and such
When your loop runs, it’s only running through once & that’s it, because it’s really all you gave us so you never create another loop, or store one into a function to keep checking that it’s gonna change or not
Thankfully though, we can use the GetPropertyChangedSignal method that’ll detect when a specific Property of that Instance changes, and we connect this to a function here so keeping that in mind:
--\\ Variables
local part = game.Workspace:WaitForChild("partModel").part
--\\ Functions
-- Now instead of creating a loop, make a function here instead
local function ChangePartColor()
if Part.Transparency == 0 then
while part.Transparency ~= 1 do
part.BrickColor = BrickColor.random()
task.wait(1)
end
end
end
part:GetPropertyChangedSignal("Transparency"):Connect(ChangePartColor)
What we did here, is check for a valid property of our Instance, and connect it to an Event so that it fires whenever that specific property changes using our ChangePartColor function
I also added in a conditional if statement, as I’m unsure what you’re exactly doing to the part (Tweening, Instant Change, etc) and it’ll make sure that our Part’s Transparency is equal to 0, so that we can start our loop
You also don’t need to break the loop at all, as it’ll automatically stop looping the moment the part's Transparency changes back to “1”
That worked, thank you! Quick followup question. I noticed that the GetPropertyChangedSignal didn’t always activate immediately, and would sometimes take up to 10-15 seconds before triggering. I am guessing that may be due to server lag on my test box. Is that normal?
That’s because it’s not calling the function when the script runs, it only calls the function when its transparency changes. I would add ChangePartColor() at the end of the script to make it work when the script runs.