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

Why this prints out 20+ times when I want it once?

Asked by
TechModel 118
3 years ago
Edited 3 years ago

This script is disabled on purpose, but when the other script is activated, it creates a "part" scaled 60,60,60 radius and it is a kill zone purpose. when it enters the zone it does 2k damages, but checking the output, I see 20+ "damage" prints and what gives a whooping 40k damage and that's too much. How do I fix this script? Thanks.

And when it does go over 2k damage, the health bar glitches and there's this long red line above my screen through the roblox default health bar.

--Kill OnTouch

function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) then   -- if a humanoid exists, then
        print("damage")
        humanoid:TakeDamage(2000)
end 
end

script.Parent.Touched:connect(onTouch)

3 answers

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

Thats because the character has multiple parts that can each touch the part mutliple times causing the function to constantly run. To solve this you can add a debounce so the code can only run once every few seconds.

Edited so there is a debounce for each individual character:

--Kill OnTouch
function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid") 
    if (humanoid ~= nil) and not part:FindFirstChild("Debounce") then   -- if a humanoid exists, then
        print("damage")
    local DB = Instance.new("StringValue", part.Parent)
    DB.Name = "Debounce"
        humanoid:TakeDamage(2000)
    wait(3) -- waits 3 second before it can deal damage again
    DB:Destroy()
end 
end

script.Parent.Touched:connect(onTouch)
0
But then how do I want it to kill multiple people? When I placed humanoids in the kill zone, only one dies TechModel 118 — 3y
0
Then you should add a debounce for each individual player. This can be accomplished in mutliple ways but I like to add an instance to the Player that touched it. JustinWe12 723 — 3y
0
Did some research and all I seen was people discussing about dictionary's and creating tables something to do with remote event. I don TechModel 118 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

The reason this might be happening is that multiple parts of the body touch the kill zone to do this you need to add something called a debounce.

local Debounce = false

function onTouch(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if Debounce == false then
    Debounce = true
         if (humanoid ~= nil) then
                      print("damage")
              humanoid:TakeDamage(2000)
          end   
    wait(SETDELAYHERE)
    Debounce = false
end)

script.Parent.Touched:connect(onTouch)
Log in to vote
0
Answered by 3 years ago

its basically what everyone above said, there is no limit o how many times an object can me touched, its kind like while true do loop with no wait (ouch), so ur gonna need to use debounce, which is pretty easy

Answer this question