Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why my "While wait() do" loop only works for once for really no reason?

Asked by 2 years ago

Hello guys,

I have an issue with my script which is changes the Frame's Background Transparency in StarterGUI that slowly decreases it's transparency when there's low oxygen in my game and it increases again to 1 after there's an enough oxygen again. The problem is; it works pretty well in the beginning but it never works again when the oxygen decreases again.

Here's the script:

while wait() do
    if game.Workspace.OxGenPlrDying.Value==true then
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
        if game.Workspace.OxGenRestartComplete.Value==true then
        game.Workspace.OxGenPlrDying.Value=false
        game.Workspace.OxGenPlrHealing.Value=true
        end
    elseif game.Workspace.OxGenPlrHealing.Value==true then
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
        if script.Parent.BackgroundTransparency==1 then
        game.Workspace.OxGenPlrHealing.Value=false
        end
    end
end

A small note: The script's location that i'm talking about is game.StarterGui.DrownGUI.Frame.Script

0
was it a local script? Gmorcad12345 434 — 2y
0
Nope. It was a regular script. Va1t_Dev 86 — 2y
0
hmm... about the oxygen level change, how often does it change from true to false or false to true? Gmorcad12345 434 — 2y
0
and also, when the oxygen level change does it change for all players? Gmorcad12345 434 — 2y
View all comments (7 more)
0
if so you can consider using :GetPropertyChangedSignal() instead Gmorcad12345 434 — 2y
0
Does the value change actually work for all players? Jo1nts 134 — 2y
0
so I have an oxygen generator and it gets broken in random minutes. When it's broken, someone should restart it in 80 seconds or it'll kick everyone in the server. During the moment while it's broken, everybody's screen goes slowly red and when someone restarts it successfully, the red Frame that I'm talking about rapidly fades out. Va1t_Dev 86 — 2y
0
and after someone fix the generator, the loop starts all over again and the loop lasts forever. But the problem is, the script which increases/decreases the Frame transparency only works once. Va1t_Dev 86 — 2y
0
the logic is basically when the Generator goes into a malfunction, oxygen level inside the submarine decreases every second and it makes everyone's screen turn into red slowly because they're losing oxygen. When it's completely red, it means you're drowned and then it kicks everyone in the server (after 80 seconds). And yes, it affects everyone. I hope I explained well! Va1t_Dev 86 — 2y
0
As our friend GameStealerKid asked, can you please provide the full code? Xyternal 247 — 2y
0
aight check my answer now Va1t_Dev 86 — 2y

3 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

You mentioned that it was a regular script in a Frame...

Before I share my answer I'll share with you an experience i had with doing the same thing:

When I click on a part, a gui (The frame) is supposed to pop up... however it pops up only ONCE. After I closed it, it never pops up again. I think the logic is the same behind this.

Therefore, I used RemoteEvents and put the script in a local script instead, and it worked perfectly fine after that. I have a way out here that (possibly) works

[[EDIT]]after the comments:

you mentioned that the real problem is really only about the Gui and the transparency not working.

that is because your old script was a Server-sided script inside a Frame. This cannot be used to change the transparency of the frame, as changing the transparency of a Frame would require a LocalScript. That's why we use the RemoteEvents to detect whether the oxygen breaking etc. changed

this is the server sided script in the workspace

local OxGenPlrDying = workspace.OxGenPlrDying
local OxGenIsOnAction = workspace.OxGenIsOnAction
local OxGenPlrHealing = workspace.OxGenPlrHealing --etc etc you get the idea
local RE = game:GetService("ReplicatedStorage").RemoteEvent
OxGenPlrDying:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenIsOnAction.Value,OxGenPlrHealing.Value)--add more parameters if you need!!!
OxGenIsOnAction:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenIsOnAction.Value,OxGenPlrHealing.Value)--add more parameters if you need!!!
OxGenPlrHealing:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenIsOnAction.Value,OxGenPlrHealing.Value)--add more parameters if you need!!!

--I think whenever each of the property changes you need to fire the local script to change the transparency
end)

The local script in the frame:



local function changeBackgroundTransparency(your_parameters) repeat wait(0.001) if game.Workspace.OxGenPlrDying.Value==true then -- remove every game.Workspace if you renamed your paremeter to OxGenPlrDying script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001 if game.Workspace.OxGenRestartComplete.Value==true then game.Workspace.OxGenPlrDying.Value=false game.Workspace.OxGenPlrHealing.Value=true end elseif game.Workspace.OxGenPlrHealing.Value==true then script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 if script.Parent.BackgroundTransparency==1 then game.Workspace.OxGenPlrHealing.Value=false end end until script.Parent.BackgroundTransparency = 1 --i'm assuming you want it to be completely invisible. end RE.OnClientEvent:Connect(function(your_parameters) changeBackgroundTransparency(your_parameters) end)

So the logic here is that when oxygen level change, you fire the remote event which triggers the background transparency. it'll keep repeating the decrease/increase of background transparency until it gets the desired level.

i hope this was what you meant by your main problem

[ EDIT 2 ]

This is the first script, local script in the frame

local RE = game:GetService("ReplicatedStorage").RemoteEvent
local function changeBackgroundTransparency(OxGenPlrDying,OxGenRestartComplete)
    while wait(0.001) do
        if OxGenPlrDying==true then
            print("Player Dying")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
            if OxGenRestartComplete==true then
                OxGenPlrDying.Value=false
            end
        elseif OxGenPlrDying==false then
            print("Player Healing")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
            if script.Parent.BackgroundTransparency==1 then
            end
        end

end
    RE.OnClientEvent:Connect(function(OxGenPlrDying,OxGenRestartComplete)
--i changed the line below AND above, I removed the .Value from each. As well as everything above, I removed the .Value . I ' m actually not very sure whether it'll affect the script or not but from what I remember it'll cause an error if you have a `.` in your parameter
        changeBackgroundTransparency(OxGenPlrDying,OxGenRestartComplete)
    end)
end

Your second script should be fine.

For the third script, any particular reason to disable it? How are you going to enable it after disabling it?

[EDIT 3] The second script, I edited it a bit.

local OxGenPlrDying = workspace.OxGenPlrDying
local OxGenIsOnAction = workspace.OxGenIsOnAction
local OxGenRestartComplete = workspace.OxGenRestartComplete
local RE = game:GetService("ReplicatedStorage").RemoteEvent

OxGenPlrDying:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenRestartComplete.Value)
end)

OxGenRestartComplete:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenRestartComplete.Value)
end)

--and if you need;

OxGenIsOnAction:GetPropertyChangedSignal("Value"):Connect(function()
    RE:FireAllClients(OxGenPlrDying.Value,OxGenRestartComplete.Value) --you may have to add a OxGenIsOnAction parameter 
end)

[EDIT 4] I edited your first script a little more. I reckon the problem should be in here since it didn't print out what's necessary.

local RE = game:GetService("ReplicatedStorage").RemoteEvent

RE.OnClientEvent:Connect(function(OxGenPlrDying,OxGenRestartComplete)
    while wait(0.001) do
        if OxGenPlrDying==true then
            print("Player Dying")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
            if OxGenRestartComplete==true then
                OxGenPlrDying.Value=false
            end
            elseif OxGenPlrDying==false then
                print("Player Healing")

 script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
            if script.Parent.BackgroundTransparency==0 or script.Parent.BackgroundTransparency==1 then break
            end
        end
    end
end)
--I may have missed out an "end" somewhere but most likely not

[EDIT 5]

I further edited your first script. Hopefully this works a bit better. Also sorry for the really late reply, was quite busy

local RE = game:GetService("ReplicatedStorage").RemoteEvent

RE.OnClientEvent:Connect(function(OxGenPlrDying,OxGenRestartComplete)
    while wait(0.001) do
        if OxGenPlrDying==true then
            print("Player Dying")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
            if OxGenRestartComplete==true then
                OxGenPlrDying.Value=false
            end
        elseif OxGenPlrDying==false then
            print("Player Healing")
        end
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
        if script.Parent.BackgroundTransparency==0 or script.Parent.BackgroundTransparency==1 then break
        end
    end
end)

[EDIT 6] Well this seems pretty challenging to me. None of the above solutions really worked... no really sure if this will now. To be honest i don't know why Player Dying and Player Healing keep printing... the only reason for that is that the BoolValue keeps alternating between false and true. At this point, really the only way is to keep trying different methods of writing your script add a remotefunction called "RemoteFunction" to your replicated storage and try this!

your first script (local script): warning this may cause lag but hopefully it doesn't

local RE = game:GetService("ReplicatedStorage").RemoteEvent
local RF = game:GetService("ReplicatedStorage").RemoteFunction

RE.OnClientEvent:Connect(function(OxGenPlrDying)

    while wait(0.001) do
        if OxGenPlrDying==true then
            print("Player Dying")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
            if workspace.OxGenRestartComplete.Value == true then
                RF:InvokeServer()
            end

        elseif OxGenPlrDying==false then
            print("Player Healing")
            script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
        end
        if script.Parent.BackgroundTransparency==0 or script.Parent.BackgroundTransparency==1 then break
        end
    end
end)

in another new script [Script 4] a Server-Sided-Script in Server-Script-Service:::::::

local RF = game:GetService("ReplicatedStorage").RemoteFunction
RF.OnServerInvoke = function()
    workspace.OxGenPlrDying.Value = false
end

i'm actually quite confident that this will work now lol, if it still doesn't that would be quite sad xD.

i deleted the first draft because when i tried to edit it said : "Whoa there! That's some awful long text. Please stay below 10,000 characters!

"

1
Thank you so much! That helps me very well. Va1t_Dev 86 — 2y
1
Yes, you put the second script in the Frame as a Local script. And yes, you can use the same script. Glad to help! Gmorcad12345 434 — 2y
1
I can't still believe such a small problem about a basic GUI Frame script caused a big confusion. I hope I explained my problem pretty well now. Va1t_Dev 86 — 2y
1
alright, i edited my answer. Gmorcad12345 434 — 2y
View all comments (43 more)
1
yes, that's what you should replace your_parameters Gmorcad12345 434 — 2y
1
I feel like I'm about to fix it, but the local script doesn't look like it's working. I've tried to put "local function changeBackgroundTransparency(OxGenPlrDying.Value,OxGenIsOnAction.Value,OxGenRestartComplete.Value)" but it gives me an error like "Expected ")" (to close '(' at column 44) got '.' " I tried deleting the ".Value", it didn't work again (but I didn't see any errors now as well). Va1t_Dev 86 — 2y
1
I've also put "local RE = game:GetService("ReplicatedStorage").RemoteEvent" in the beginning of my local script inside the Frame (because it highlighted the "RE") . I removed every "game:Workspace" before the "OxGen" values to make it work properly. I've put "print:("Player Dying") after the "if OxGenPlrDying.Value==true the" to make sure if it works or not (and it didn't print, as expected...) Va1t_Dev 86 — 2y
1
I think I did something wrong again, lol. (I've deleted my older comments because they were useless now) Va1t_Dev 86 — 2y
1
the error that happened should be an easy fix. its a matter of putting your () wrongly. can you edit your question to show your new script? and make sure i can see clearly line by line and the errors in the output Gmorcad12345 434 — 2y
1
we can bring this to discord if you would like Gmorcad12345 434 — 2y
1
Ok I updated my answer now. I hope it explains! Va1t_Dev 86 — 2y
1
in your answer below, the 3rd script, you disabled it after DisasterHappened. Doesn't it make it so that the ship can no longer malfunction? Gmorcad12345 434 — 2y
1
alright, i edited my answer. I may not be able to respond to you for the next 12 hours because its night time. Gmorcad12345 434 — 2y
1
lets just upvote each other's messages so we can notify each other that we have responded when we see our reputation change, lol Gmorcad12345 434 — 2y
1
The third script is working pretty fine. And yes, it's supposed to don't work again after DisasterHappened is true (and I'm aware of it's not set to "true" yet while I'm testing, lol). I put the exact same script you give, but it didn't work again. I also said I tried deleting those ".Value" in my updated answer too, didn't work. I can't still figure out what's wrong with the all scripts, dang... Va1t_Dev 86 — 2y
1
any errors? you can look at script analysis and output for more errors Gmorcad12345 434 — 2y
1
do you know at which line the script stops? Gmorcad12345 434 — 2y
1
i edited my answer again Gmorcad12345 434 — 2y
1
Nope, It doesn't work again. I always check the output for if there's any errors, but I can't see any. The " print("Player Dying") " is also useless because it doesn't print anything (you can take a look at my answer to learn where's the " print(" ") " function. Va1t_Dev 86 — 2y
1
I'm also pretty sure about OxGenPlrDying sets to "true" when the OxGen goes into a malfunction. I think it's about the script. Va1t_Dev 86 — 2y
1
edited my answer again. When you play the game, at when does it start malfunctioning? Gmorcad12345 434 — 2y
1
try putting script 2 at ServerScriptService. Not sure if that'll help but its better to try Gmorcad12345 434 — 2y
1
Wait a minute, in GameStealerKids' Answer, You said that it works very well for once... maybe try using HIS scripts but with RemoteEvents? Gmorcad12345 434 — 2y
1
The script that GameStealerKid talks about is my old script (the script that only works once which is I showed it in my question, so not yours), you can see he wrote his comment 2 days ago. Now the script that you give doesn't work (not even once). I don't know much about RemoteEvents and I don't know what to do now lol Va1t_Dev 86 — 2y
1
and as I said, if you look at my third script that I gave in my answer there's a "random = math.random(5,30)" and "wait(random) that means it sets the time randomly (between 5 and 30 seconds). So the time that it stars malfunctioning isn't that specific. Va1t_Dev 86 — 2y
1
Oh wait, your script that you've edited just rn works now! But it works once again. It works without any problems in the beginning (when the PlrDying value sets to "true" first time) but after that, when the value sets to "false" again after the generator is fixed, I get something like this in the output: "Players.Va1t_Dev.PlayerGui.DrownGUI.Frame.LocalScript:9: attempt to index with boolean value Va1t_Dev 86 — 2y
1
After that it keeps printing Player Healing and Player Dying at the same time forever, here's the link of the screenshot: https://imgur.com/a/HbQGZi6 Va1t_Dev 86 — 2y
1
I think it's about the Line 9 that sets the OxGenPlrDying to "false". I have no idea why it does that, lol. But I feel like we're too close to fix the problem! Va1t_Dev 86 — 2y
1
UPDATE: Ok, I fixed the "boolean" error at Line 9 by adding game.Workspace before the OxGenPlrDying.Value=false. But the problem I'm talking about is still exists. I have no idea why it does that, lol. Va1t_Dev 86 — 2y
1
The problem you meant by printing player dying and player healing printing very fast repeatedly right? Gmorcad12345 434 — 2y
1
Ok, when OxGenPlrDying == true, the background transparency of the frame increases right? when its false it decreases right? Gmorcad12345 434 — 2y
1
well i think i found your game, a disaster happened and I think i know what you mean by the red screen now Gmorcad12345 434 — 2y
1
when OxGenPlrDying is true the Frame's transparency supposed to decrease slowly (so when it's true the red screen will start becoming visible slowly) and when it's false it'll increase rapidly so the red screen will fade out in a few seconds. The logic is basically when it's true it means the player is drowning and the red screen starts becoming invisible. Va1t_Dev 86 — 2y
1
also the players' screen won't become red after a disaster happened. It'll only go red when the Oxygen Generator is broken. Va1t_Dev 86 — 2y
1
If you still didn't get it I can record a video about it and then publish it to YouTube if you want Va1t_Dev 86 — 2y
1
sorry for the really late reply! currently editing my answer rn Gmorcad12345 434 — 2y
1
I also think i found out the solution now Gmorcad12345 434 — 2y
1
alright, i edited my answer. take a look Gmorcad12345 434 — 2y
1
No problem for the late answer! But it still does the same thing, after the OxGenPlrDying.Value is false it starts to print both rapidly at the same time (Player dying and player healing). Plus after I put your code that you've edited it lastly the screen doesn't go red anymore so the Frame's transparency doesn't decrease for some reason. Va1t_Dev 86 — 2y
1
Alright, i edited my script again Gmorcad12345 434 — 2y
1
This time i added a remote function Gmorcad12345 434 — 2y
0
I'm about to go insane... https://imgur.com/a/jSiXMKA Va1t_Dev 86 — 2y
1
I really have no idea why it does that. It literally bothers us both that we're trying to fix a small bug.since 4 days.. Va1t_Dev 86 — 2y
1
So that means it's still about the LocalScript or the Server-Sided script. (And yes, the last script you give lags the whole game lol.) Va1t_Dev 86 — 2y
1
OH GOD, I LITERALLY FIXED THE WHOLE THING MYSELF LOL XD I just edited my old script (which was a normal script inside the Frame) and it works pretty well now! (And yes, not only once, it works forever now!) Va1t_Dev 86 — 2y
1
If you want to see what did I changed, check my answer below that I just edited! Va1t_Dev 86 — 2y
1
wow, good that its all settled. Gmorcad12345 434 — 2y
Ad
Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

If you don't already have the answer it's because when oxygen restarts, the oxygen restart value is true so when you try it again it's still true and stops everything so use this. (I think this will work)

while wait() do
    if game.Workspace.OxGenPlrDying.Value==true then
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
        if game.Workspace.OxGenRestartComplete.Value==true then -- This value doesn't become false again
        game.Workspace.OxGenRestartComplete.Value = false -- This is what you need to do to make it stop restarting
        game.Workspace.OxGenPlrDying.Value=false
        game.Workspace.OxGenPlrHealing.Value=true
        end
    elseif game.Workspace.OxGenPlrHealing.Value==true then
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01 
        if script.Parent.BackgroundTransparency==1 then
        game.Workspace.OxGenPlrHealing.Value=false
        end
    end
end

Hope I could help!

0
Thanks for the answer but sadly it still doesn't work. Actually it works, but only for once (it does the same thing as my own script, so nothing changed). It works very well in the first time but when the Generator goes into a malfunction and needs to restart again, it never works. (I'm pretty sure the other scripts are correct (and the values also too), the problem is probably about the script it Va1t_Dev 86 — 2y
0
Could you send the other scripts so I could check them out. It might help GameStealerKid 79 — 2y
0
(Just to make sure) GameStealerKid 79 — 2y
0
check my answer Va1t_Dev 86 — 2y
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

I knew that the problem wasn't supposed to be that huge and confusing...

I fixed the problem myself by a couple small changes in my old script. I was pretty tired and I couldn't figure out what's the problem in the day that I asked this question.

But you may be asking right now, how did I fix it? I already knew that I'm not supposed to have another variable or something more confusing in my script. So, I decided to look at to my old script again and made some small changes.

As I told people in my question, the problem was about the script were only working for once. I tried to find what's the problem again just a few minutes ago, and I fixed it in a few minutes.

I'm so tired today and I really can't tell what did I change to fix the script rn. I'll just copy+paste my script right here, so you can check it out.

while wait(0.001) do
    if game.Workspace.PlrDyingIngition.Value==true then
    if game.Workspace.OxGenPlrDying.Value==true then
        print("Player Dying")
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency-0.0001
        if game.Workspace.OxGenRestartComplete.Value==true then
            game.Workspace.OxGenPlrDying.Value=false
        end
    elseif game.Workspace.OxGenPlrDying.Value==false then
        print("Player Healing")
        script.Parent.BackgroundTransparency=script.Parent.BackgroundTransparency+0.01
        if script.Parent.BackgroundTransparency>0.99 then
        game.Workspace.PlrDyingIngition.Value=false
    end
    end
    end
end

I'm really sorry for people who's trying to help me since 4 days. It was completely my fault and my stupidity. But I'm pretty happy and relaxed when I fixed that trouble which was bothering us all. Have a good day all!

Answer this question