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

Sometimes my sword tool works sometimes it doesn't take damage, whats wrong?

Ok so my sword script is being weird. Sometimes my sword takes damage on the player who takes it and sometimes it doesn't and says "Humanoid is not a valid member of Workspace" does anyone know what is wrong with my script?

My script:

script.Parent.DamagePart.Touched:Connect(function(p)
	if script.Parent.CanDamage.Value == true then
	script.Parent.CanDamage.Value = false
	p.Parent.Humanoid:TakeDamage(10)
	wait(0.8)
	script.Parent.CanDamage.Value = true
	end
end)



1 answer

Try checking if p is a player first because p could be anything really. When you're not sure if something is there, use :FindFirstChild, so hopefully it doesn't error and it will tell you if that object is there.

script.Parent.DamagePart.Touched:Connect(function(p)
	if script.Parent.CanDamage.Value == true then
		script.Parent.CanDamage.Value = false
		local humanoid = p.Parent:FindFirstChild("Humanoid")
		if humanoid then -- checks if the parent of the hit has a Humanoid. If it does, then it's a player
			p.Parent.Humanoid:TakeDamage(10)
			wait(0.8)
			script.Parent.CanDamage.Value = true
		end
	end
end)

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