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

How to Temporarily Stop a Script After it Has Ran?

Asked by 8 years ago

I am trying to create a tool dispenser, thank you dyler3, essentially a giver, that would clone a tool into the players backpack when a certain part is touched. Only after testing it the script clones multiple tools into my backpack and I wanted to know how to stop the script from running after the player had received a single tool. What would I include? Here is the script:

script.Parent.Touched:connect(function(Part)
    if Part.Parent:FindFirstChild("Humanoid") then
        local player=game.Players:GetPlayerFromCharacter(Part.Parent)
        game.ReplicatedStorage.Tool:clone().Parent=player.Backpack
    end
end)

Could I possibly make it check the players backpack for the specific tool and if it does find the tool, wait 2.5 seconds using an if, then statement beforehand? I tried something like this:

script.Parent.Touched:connect(function(Part)
    if Part.Parent:FindFirstChild("Humanoid") then
        if player.Backpack:FindFirstChild("Tool") then
        wait(2.5)
        local player=game.Players:GetPlayerFromCharacter(Part.Parent)
        game.ReplicatedStorage.Tool:clone().Parent=player.Backpack
    end
end

But then that would mean that you would have to already have the tool to get the tool. How would I exclude lines 5 and 6 from the statement on line 3 and 4; how would I run it separately of that, yet beforehand?

Or could I possibly stop the script from creating another thread for a specific amount of time? So only one thread would run at a time and there would also be included a sort of cooldown?

0
Post the script. Discern 1007 — 8y
0
Have you tried using a Debounce? A Debounce is used to prevent a Function from firing multiple times on Execution. Also, there is a Documentary about this subject on the Roblox WIKI. :) Wiki Documentary on Debounce: http://wiki.roblox.com/index.php?title=Debounce TheeDeathCaster 2368 — 8y
0
I did try using debounce, but I am not sure if I am using it correctly, because it did not work. Brisingr360 45 — 8y
0
Could somebody show me how to properly use 'debounce' for this situation so I could check it and make sure I used it right? Because like I said earlier... Brisingr360 45 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

This Answer is based off of my Comment:

What is a Debounce?

A Debounce is something in ROBLOX which is primarily often used for specific or certain tasks (primarily the .Touched Event), in which prevents a Function from firing multiple times on Execution.

What is it primarily used for?

It is primarily used to prevent an Event or Function from firing multiple times on Execution, as for example, it is primarily used for the .Touched event, to prevent it from firing multiple times on Execution.

How can I use it?

Once you get used to using it, it's actually quite easy to use after a while of using it. :D As for example, the above code:

local Debounce = true --Here is our Debounce; this will be used to prevent the code below from firing multiple times on Execution

function SomeFunctionIDontKnowWhatNameToUse() --Lol, what a name :P ;We have now created a new function, which will be used later on; This function will use the 'Debounce'
    spawn(function() --Sorry for using a 'Coroutine', but, without it, the code will not work properly for this example :(
        if not Debounce then return end --If 'if' statement is now checking if the 'Debounce' is true, and if so, will allow the following chunk to run
        Debounce = false --Sets 'Debounce' to false, starting the Debounce; Since 'Debounce' is now false, when the next 'if' statement checks to see if it equals to 'true' (or if 'Debounce' returns true), it will see that 'Debounce' returns false, thus preventing the Function from going on any further
        print("I fired inside a function which uses a Debounce! :D") --This will print into the Output, if the 'if' statement allowed the Chunk to run (if the Debounce is/was true)
        wait(2) --Waits a specific amount of time before running the next Chunk
        Debounce = true --Reverts 'Debounce' back to true, allowing the Function to fire again
    end) --Ends the chunk for the 'Coroutine'
end --Ends the chunk for the function

for i = 1, 6 do --This will run the following chunk 6 times
    print("For loop fired! :D") --This will print into the 'Output' every time the 'for' loop has looped (or fired)
    SomeFunctionIDontKnowWhatNameToUse() --Calls the function, thus the Debounce begins, and the Function executes
    wait(1) --Waits 1 second before allowing the 'for' loop to loop again
end --Ends the chunk for the 'for' loop

Now, when we check the Output:

--Output:
--For loop fired! :D
--I fired inside a function which uses a Debounce! :D
--For loop fired! :D
--For loop fired! :D
--I fired inside a function which uses a Debounce! :D
--For loop fired! :D
--For loop fired! :D
--I fired inside a function which uses a Debounce! :D
--For loop fired! :D

And, Wah-La! Our Debounce works! :D

But, this is just one of the many example for how Debounce works, it can be used for a lot more purposes, and is highly recommended to use! :D

Information left out

1. The Output - The Output is something all (or, almost) Lua programs have; It receives and returns the Feedback back to the user, whether it be an Error, the Print function firing, ect.

2. The For Loop - (This is based off the for loop I used in the code) The For loop is, well, a loop, that is used to fire multiple for a certain amount of loops, as the WIKI says, The for loop is a way of running a command or set of commands a set number of times. The basic syntax is as following: for iterator_variable = start value, end value, increment do, in other words, it will fire the Chunk a specific number of times, then stop the loop.

3. The If statement - The If statement is used for almost all Codes in all Lua programs: It is used to check if a Condition is equal to an (or it's) Argument, or if the Condition returns true, and if the Condition does return true, then it will run a specific chunk, but, if it returns false, then it will run a separate chunk, if you use the else statement along with it, but, without the else statement (or elseif statement), it will not run anything (the else and elseif statements are optional to use for if statements).

Important Fact

This is based off of your other Question.

It is a very, very bad idea to use the Disabled property of a script to activate a Debounce, because, once it Disables itself, it won't beable to Enable itself again, because, when it disables itself, it stops all execution of the code (kind of like how using the break keyword stops the Loop, and doesn't run anymore code (it returns, and doesn't return the next chunk)).

Hope this helped!

0
Thank you so much! Your explanation really helped and I really appreciate it, along with all the other people that did input effort to help me with this! Thanks again. :D Brisingr360 45 — 8y
0
No problem man! :D When I decide to Answer a Question, I try my best to explain as best as I can! :D Glad to help. :) TheeDeathCaster 2368 — 8y
Ad
Log in to vote
0
Answered by
drahsid5 250 Moderation Voter
8 years ago

This is really simple,

after the tool is given you can either use

wait(#)

or

repeat wait() until player.BackPack:FindFirstChild("Tool")

Though if it's a .Touched function, it will probably fire the function every time it's touched, and have it run on a different thread.

0
I am sort of new to scripting, but is it possible to make a script run on a single thread? Like until that thread stops running? Brisingr360 45 — 8y

Answer this question