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

I'm doing this code to fade a message. Is there a way to optimize it?

function destroyMessage() wait(5) while true do script.Parent.Frame.BackgroundTransparency = script.Parent.Frame.BackgroundTransparency + 0.05 script.Parent.TextLabel.TextTransparency = script.Parent.TextLabel.TextTransparency + 0.05 wait(0.1) end

end

destroyMessage()

2 answers

✓ Accepted answer

This might help you. I've been lately seeing that (Don't quote me. I'm sometimes wrong) that wait() isn't as good as using a yield or things to that effect. (Could still be wrong)

function destroyMessage() 
wait(5) 
while true do 
script.Parent.Frame.BackgroundTransparency = script.Parent.Frame.BackgroundTransparency + 0.05 
script.Parent.TextLabel.TextTransparency = script.Parent.TextLabel.TextTransparency + 0.05 wait(0.1) 
end
end

destroyMessage()

That is your original code. I dislike it for a variety of reasons. Firstly, it's not recommended to keep a loop within a function. Secondly, this code is terrible (sorry I had to). It won't do what your looking for. It will run infinitely. Do you know at what point you want the transparency to go to? Assuming you want it at 1, you can easily achieve this with something like:

function destroyMessage() 
while script.Parent.Frame.BackgroundTransparency ~= 1 do 
script.Parent.Frame.BackgroundTransparency = script.Parent.Frame.BackgroundTransparency + 0.05 
script.Parent.TextLabel.TextTransparency = script.Parent.TextLabel.TextTransparency + 0.05 wait(0.1)
end
destroyMessage()
end

A disclaimer. I've put waits in here, as its good enough for this. You can get nitty gritty and use different methods of making your script wait, but for most cases using wait() is perfectly ok.

Now onto what I've done to your code. Firstly, I removed the 5 second delay from the function start. It made no sense to put there. Secondly, I added a conditional statement to make your loop end. I still don't like it, but its good enough and keeps what you wanted to achieve in mind. The next part I added was moving the destroyMessage() function up into the function. Leaving it outside of it doesn't do anything, and it will now run once the loop is over.

Hope it helps!

You put wait(5) which is going to have the script wait 5 seconds before it does the while true do loop. lower the wait time and that should work

5 comments

0

um that does nothing to optimize

0

and a lot more things that he can optimize

0

it kinda is optimizing. it makes the script more efficient in speed by lowering wait time

0

that doesnt make it more efficient. if anything it makes it less efficient to lower the wait time, because it's running more

0

he put the wait time before the loop started. if he put it after the loop started then yes it would be more efficient. Seems like you read the code wrong by mistake.

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