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

How can I stop this script from doing something if it has been hit?

Asked by
Faazo 84
6 years ago

I'm trying to make a script where if the player hits an object, it will change the value of something to true, then wait 5 seconds before it changes it back to false. I want it to wait another 5 seconds if it has been hit before it gets a chance to change the value back to false. However, I'm having trouble with this. My current script continues to change the value to false, even if I hit it again. (It's supposed to wait 5 seconds)

local Animal = script.Parent
local Attacker = Animal.Attacker
local TakingDamage = Animal.IsTakingDamage

script.Parent.Touched:Connect(function(tuc)
    if tuc.name == "Blade" then 
        Attacker.Value = tuc.Parent.Parent.Name
        TakingDamage.Value = true
        print("TakingDamage")
        wait(5)
        Attacker.Value = "nil"
        TakingDamage.Value = false
    end 
end)

How can I stop it from setting the value false if it has been hit again? Feel free to ask questions. Help is appreciated.

3 answers

Log in to vote
1
Answered by
aazkao 787 Moderation Voter
6 years ago
Edited 6 years ago

At first i thought it was just a simple debouncing that was required but after re-reading your question this was a more interesting problem.

I added a while loop and a waitTime, basically the waitTime will reset back to 5 seconds if the thing was touched before waitTime counts down to 0 or if the waitTime already reaches 0

If the waitTime reaches 0 it will toggle the Touched value to false, allowing the thing to be touched again

My solution:

local Animal = script.Parent
local Attacker = Animal.Attacker
local TakingDamage = Animal.IsTakingDamage
local Touched  = false
local breaker = false
local OriginalWaitTime = 5--change this value to however long you want it to wait in seconds
local waitTime = 5

script.Parent.Touched:Connect(function(tuc)
    breaker = true
    if tuc.name == "Blade" and not Touched then 
    Touched = true
     breaker = false
    Attacker.Value = tuc.Parent.Parent.Name
    TakingDamage.Value = true
    print("TakingDamage")

    end 

while waitTime>=0 do
    waitTime = waitTime - 1

    if breaker then--if the thing has been touched inappropriately this will trigger and end the loop and a new while loop is created and resets the waititme back to 5.
        waitTime = OriginalWaitTime
        breaker = false
        break
    end

    if waitTime <0 then--after 5 seconds Touched becomes false and player can touch again
        Touched = false
        breaker = false
        waitTime = OriginalWaitTime       
         Attacker.Value = "nil"
        TakingDamage.Value = false
        break 
    end

    wait(1)
end

end)
0
Uhm, this is MY solution. AIphanium 124 — 6y
0
Uh not really, you think i dont know to use a variable for wait? I came up with this myself bro, its not that hard. Also your script has logical errors and your while loop runs indefinitely, my loops only run if it needs to, much more efficient and everything is contained within the script, no value object needed aazkao 787 — 6y
0
Heh, funny, we both know that i was the first that found the solution, oh and also, the longer the script is, the MORE lag it produces. AIphanium 124 — 6y
0
*facepalm* aazkao 787 — 6y
View all comments (13 more)
0
Well, sorry pal, but i don't see any "Logical Errors", except the existence of this "Copycat". AIphanium 124 — 6y
0
If you don't believe in the fact that a script is capable of producing lag, well, its your problem, 'cause its proved. AIphanium 124 — 6y
0
Alright fine if you think my programming skills isnt good enough to come up with this solution by myself then so be it, i cant really change your mind. aazkao 787 — 6y
0
"don't believe in the fact that a script is capable of producing lag," bruh, when did i even say or imply that aazkao 787 — 6y
0
I didn't say that "your programming skills isn't good enough to come up with this solution", our answers are similar, but i was the one who found the solution first. AIphanium 124 — 6y
0
Alright cool, then i guess it was a misunderstanding, especially when you said " Uhm, this is MY solution." aazkao 787 — 6y
0
jesus christ can you grow up, i admitted it was a misunderstanding and you still have to say "have fun with your solution" aazkao 787 — 6y
0
Accepted, lets end this. AIphanium 124 — 6y
0
Alright we cool? We have different ways to achieve the solution,its up to the OP which one he chooses aazkao 787 — 6y
0
Both of you chill the f out. It doesn't matter, no one is a "copycat". So stfu and go back to scripting SBlankthorn 329 — 6y
0
Tried this script and it works. Thank you! Faazo 84 — 6y
0
No problem bro, this was an interesting problem, i always used debounce but never thought to use debounce in a way that resets itself if a player triggers the touched event aazkao 787 — 6y
0
Welp, congratulations, i wasted 15 minutes to write my solution. AIphanium 124 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

what you want is a debounce, which is pretty much a bool variable like debounce = false and you name debounce anything like died = true, dying = false like this example:

local debounce = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debounce then -- the if statement only executes the code if "debounce" is false
        debounce = true -- makes it so we cant execute the code and sets it true
        print("hello")
        wait(5) -- waits 5 sec til we can execute the code
        debounce = false -- sets it to false
    end
end)

like this

local Animal = script.Parent
local Attacker = Animal.Attacker
local TakingDamage = Animal.IsTakingDamage
local canTouch = false

script.Parent.Touched:Connect(function(tuc)
    if tuc.name == "Blade" and canTouch then
    canTouch = true
        Attacker.Value = tuc.Parent.Parent.Name
        TakingDamage.Value = true
        print("TakingDamage")
        wait(5)
        Attacker.Value = "nil"
        TakingDamage.Value = false
    canTouch = false
    end 
end)

also there can be other forms of a Debounce like using numbers such as like this

debounce = 0

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debounce == 0 then  
        debounce = 1
        print("hello")
        wait(5) 
        debounce = 0
    end
end)
0
that doesnt work because i need it to always wait 5 seconds after its touched.If I use a debounce it only waits 5 seconds after the first touch and any touch after that wont do anything. Faazo 84 — 6y
0
but you said you didnt want it to set it to true if you touched it until its set to false which a debounce is pretty much for User#23365 30 — 6y
1
No, that's not what he means, I don't think. I'm a bit new to scripting, so I don't know how to code it, but an example of this problem would be - The brick gets hit. Four seconds later, and one second before the wait ends, the brick gets hit again. However, the wait ends one second after that hit instead of 5 seconds after that hit, like he wanted. Karasu_H 28 — 6y
0
@Faazo, the script works, at least the idea behind it, expodo did the debouncing wrong, the debounce will toggle after 5 seconds and you can touch the brick again. just change "and canTouch" to "and not canTouch" aazkao 787 — 6y
View all comments (2 more)
0
Itami is right. If I hit it and then wait four seconds then hit it again, the wait would still end a second later, because it's waiting on the first hit. Faazo 84 — 6y
0
OH i get what you mean now aazkao 787 — 6y
Log in to vote
0
Answered by
AIphanium 124
6 years ago
Edited 6 years ago

You can use a quite simple method;

  • create a simple Script.

  • then add an IntValue named Cooldown inside the script.

  • add the lines below to the Script!


--  The Power Of "Events"!
local Animal = script.Parent
local Attacker = Animal.Attacker
local TakingDamage = Animal.IsTakingDamage
local cool = script.Cooldown
loop = false

    script.Parent.Touched:Connect(function(hit)
  if hit.Name == "Blade" then
Attacker.Value = hit.Parent.Parent.Name
        TakingDamage.Value = true
cool.Value = cool.Value + 5
loop = true
end
end)

cool.Changed:Connect(function()
if cool.Value == 0 then
TakingDamage.Value = false
Attacker.Value = nil
loop = false
wait(1)
end
end)

while loop == true do
wait(1)
cool.Value = cool.Value - 1
end

-- Feel free to edit it!

-- Hope i helped.

---- AIphanium.

0
The idea behind this is valid, but the script itself doesn't work. Faazo 84 — 6y
0
its because of line 12, that was the logical error i was talking about lol, and firing the changed event every second isnt going to help a whole lot in server lag, especially when i think the OP is going to use this script multiple times in different objects aazkao 787 — 6y

Answer this question