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

how would i make it so while a bool is true, the tool waits 5 and then activates?

Asked by
Stea_my 48
3 years ago

basically, i'm trying to make it so when a bool value is true, it waits 5 seconds and then continues what the tool was going to do, but when its false, it instantly breaks and stops what it was going to do, but i just cant figure it out, it either does it instantly or doesn't do it at all, instead of waiting 5 and then doing it and breaking, any fixes? this is my code

holding.Changed:Connect(function()
    if holding.Value == true then       
            for i = 0, 5 do
                if holding.Changed then
                    print("ended prematuraly")
                    return
                else
                    print("healing in progress")
                    wait(1)
                end
            end
        end

after all the ends the "if holding.Value == true then" continues and heals the player

1 answer

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
3 years ago
Edited 3 years ago

The way i'd probably go about it is a loop like this

local bool = Instance.new("BoolValue") -- whatever bool you are tracking
local wasFalse = bool.Value -- temporary value

for i=1,5 do
    wait(1)
    wasFalse = wasFalse and bool.Value
end

if wasFalse then
    -- your healing code
end

The way this works is if bool is true then wasFalse is just set to true, but if bool is false wasFalse gets set to false. Since wasFalse is now false it cannot be true again because we use an and statement.

Of coarse one problem is that if the value is set to false in between checks then it wont register, you can fix this by increasing the check interval.

Couldn't think of a good way to explain it, im happy to answer any questions.

0
well i just tested it and what its doing is waiting 5 seconds and working, but if i stop holding click, it keeps going, what i'm trying to do is make it so when you stop holding click, it returns back to waiting for the function to be called and doesnt execute the healing code Stea_my 48 — 3y
0
You shouldnt need to make it stop since, I assume, you put it in an event like you did with your own code. Benbebop 1049 — 3y
0
If you didnt I probably should have mentioned that Benbebop 1049 — 3y
0
basically how my code runs is, there is an event that fires everytime an input starts/ends using userinputservice, and detects if the player is holding mouse1, and then turns the bool on/off Stea_my 48 — 3y
View all comments (7 more)
0
the bool is a BoolValue object correct? Benbebop 1049 — 3y
0
yeah its the same, do you want me to send you the client/server parts of the bool on/off switch? Stea_my 48 — 3y
0
I think I know what is going wrong, let me fix the code Benbebop 1049 — 3y
0
I just added the proper stuff for the BoolValue, now all that you need to do is replace "Instance.new("BoolValue")" with whatever BoolValue you are using Benbebop 1049 — 3y
0
still does the same thing, very strange Stea_my 48 — 3y
0
Yea, ill have to test it myself when i get the chance. Benbebop 1049 — 3y
0
alrighty then, tell me when you test it and if theres anything wrong on your end Stea_my 48 — 3y
Ad

Answer this question