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

Script is working fine, output still says I have errors?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

The script functions perfectly, only that it spams the developer console and the output to the point that I lag out or I can't find the error that I'm looking for. Here's the script, tell me what I can change to stop it from erroring.

PS THE SCRIPT WORKS FINE, JUST HELP ME WITH THE ERRORS

Parent = script.Parent
enabled = false
-- Variables

Parent.Touched:connect(function(hit)
    player = game.Players:GetPlayerFromCharacter(hit.Parent)
    wait()
    if not enabled and hit.Parent ~= nil then
        enabled = true
        if not player and hit ~= nil and hit.Parent:findFirstChild("Humanoid").Health > 0 then
            if hit.Parent:FindFirstChild("Humanoid") then
                hit.Parent:WaitForChild("Humanoid").Health = hit.Parent:WaitForChild("Humanoid").Health - 20
            end
        elseif player and player.TeamColor ~= BrickColor.new("Black") then
                if hit.Parent:FindFirstChild("Humanoid") then
                hit.Parent:WaitForChild("Humanoid").Health = hit.Parent:WaitForChild("Humanoid").Health - 20
            end
        end
    end
    wait(3)
    enabled = false
end)
09:21:09.768 - Workspace.FastRobloxian.Right Arm.Damage:10: attempt to index a nil value
09:21:09.769 - Stack Begin
09:21:09.769 - Script 'Workspace.FastRobloxian.Right Arm.Damage', Line 10
09:21:09.770 - Stack End

That's the error

0
if the script has errors, then it doesn't work fine... HungryJaffer 1246 — 8y
0
The thing is, the script DOES damage the humanoid, the debounce works perfect, but this error thingy is clogging up the output. laughablehaha 494 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

The problem is probably occurring when the arm touches something that doesn't have a humanoid in it. You could fix it like this:

Parent = script.Parent
enabled = false
-- Variables

Parent.Touched:connect(function(hit)
    player = game.Players:GetPlayerFromCharacter(hit.Parent)
    wait()
    if not enabled and hit.Parent ~= nil then
        enabled = true
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if not player and hit ~= nil and humanoid and humanoid.Health > 0 then
        humanoid:TakeDamage(20)
        elseif player and player.TeamColor ~= BrickColor.new("Black") then
                if hit.Parent:FindFirstChild("Humanoid") then
                hit.Parent.Humanoid:TakeDamage(20)
            end
        end
    end
    wait(3)
    enabled = false
end)

This will check to see if a humanoid exists before checking if its health is above 0. I also used TakeDamage(); this will take force fields etc. into account.

Events are run separately from the rest of the code, and therefore will not break the main code, nor will they be disconnected, meaning they will continue to occur.

0
Thanks Bro! :D laughablehaha 494 — 8y
Ad

Answer this question