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

How do I make the damage work and stop the infinite yield?

Asked by 4 years ago
local debounce = false

workspace:WaitForChild("Fireball").Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
        debounce = true
        hit.Parent.Humanoid:TakeDamage(3)
        wait(1)
        debounce = false
    end
end)

2 answers

Log in to vote
0
Answered by 4 years ago

It would help if you showed the output that tells you there is an infinite yield.

However, the only thing I see here that could potentially cause an infinite yield is workspace:WaitForChild("Fireball"). For this to cause an infinite yield, that would mean that there is no direct child of workspace named "Fireball".

To fix this, I would recommend putting this script inside the part that you want to deal damage, and replace workspace:WaitForChild("Fireball") with script.Parent. This would eliminate any yielding in your code, and you'd have a guaranteed reference to the part you want (given the script is parented to the part).

P.S. Given that your part's name is "Fireball", I would assume that this is a projectile of some sort that is created on-the-fly and is not a direct child of workspace at the same time this script starts running. In that case, I would still recommend the given solution, however it might require extra steps to implement. If that is the case, and you still require help, we'll need some more context to help you.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Currently your code is waiting for fireball once, then defining a function for that event for that specific fireball and moving on. You'd want to put it in a loop, instead. Infinite yield isn't an error, it's just a mistake coming from the way you've designed this fireball.

local debounce = false

while true do
    workspace:WaitForChild("Fireball").Touched:Connect(function(hit)
        if hit.Parent:FindFIrstChild("Humanoid")  then
            if debounce == false then --using and in an if statement never seems to work in my studio
                debounce == true
                hit.Parent.Humanoid:TakeDamage(3)
                wait(1)
                debounce = false
            end
        end
    end
end

To get rid of the infinite yield, you'd do something like this. However it is not the most efficient of methods.

local debounce = false

while wait() do
    if game.Workspace:FindFirstChild("Fireball") ~= nil then
        game.Workspace:FindFirstChild("Fireball").Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                if debounce == false then
                    debounce == true
                    hit.Parent.Humanoid:TakeDamage(3)
                    wait(1)
                    debounce = false
                end
            end
        end
    end
end

I didn't verify these so there might be an error, but it's not highly likely.

Answer this question