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

Why is this endlessly killing?

Asked by 4 years ago

Hi, I am confused on something:

I am making a tool that when activated, it detects for another player hitting the player who activated it and if it does detect someone, it take 30 health away. But, when activated once, it constantly detects for it. Here is my script:

01--Outside Variables--
02local Animation = script.Parent:WaitForChild("Animation")
03local cd = false
04script.Parent.Activated:Connect(function()
05    if cd == false then --check for cooldown
06    --first code--
07        cd = true      
08    --variables--
09        local Humanoid = script.Parent.Parent:WaitForChild("Humanoid")
10        local loadedAnimation = Humanoid:LoadAnimation(Animation)
11        local Root = script.Parent.Parent:WaitForChild("HumanoidRootPart")
12        local sound = script.Parent:WaitForChild("OverDrive")
13    --code--
14        sound:Play()
15        loadedAnimation:Play()
View all 24 lines...

How can I fix this? Thanks.

0
You used the debounce in the wrong place. Dovydas1118 1495 — 4y
0
Where should i use it? User#34345 0 — 4y
0
You might need to increase wait, 0.05 is very fast and in this case staying on it for 0.2 seconds is a instant kill. jwklong 32 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Actually, I used to do this and got the exact same problem as you. Here's what I did:

01--make a table for all the players that the tool touched
02 
03local TouchedPlayers = {}   --this variable should go at the top of the script before the main code is written
04 
05Root.Touched:Connect(function(hit)
06            local char = hit.Parent
07        local Humanoid = char:WaitForChild("Humanoid")
08            Humanoid.Health = Humanoid.Health - 30
09        TouchedPlayers[char] = true --tell the script that, "Hey, I just touched this person and I should not touch it again unless cooldown is over"
10        wait(4) --change it to your preffered cooldown time
11        TouchedPlayers[char] = nil --cooldown is over
12end)

and at line 5, you could do:

1if cd == false and not TouchedPlayers[char] then
2    --stuff happens here
3end

Because you need to check whether there is TouchedPlayers[char] in line 5, the Root.Touched function should be moved up the script.

Ad
Log in to vote
0
Answered by 4 years ago

What was your script?

is your script located in workspace

0
oh, it was in a tool as a script (not localScript) User#34345 0 — 4y

Answer this question