Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

if the script has errors, then it doesn't work fine...

0

The thing is, the script DOES damage the humanoid, the debounce works perfect, but this error thingy is clogging up the output.

1 answer

✓ Accepted answer

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.

1 comment

0

Thanks Bro! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.