heya. i've got a problem with a sword script i've been working on. i'm trying to make the blade of the sword do damage when ANOTHER player touches it, not the one holding it.
whenever the sword hits something, this error appears:
22:36:53.119 - Players.peasantvonpeasant.Backpack.WoodenSword.DamageForSwords:8: attempt to index a nil value 22:36:53.120 - Stack Begin 22:36:53.120 - Script 'Players.peasantvonpeasant.Backpack.WoodenSword.DamageForSwords', Line 8 22:36:53.120 - Stack End
my script is:
local canattack = true script.Parent.Blade.Touched:Connect(function(p) if canattack == true then if p:FindFirstAncestorWhichIsA("Model") then if p.Parent:FindFirstChildWhichIsA("Humanoid") then p:FindFirstChildWhichIsA("Humanoid"):TakeDamage(9) wait(1) canattack = true end end end end)
if something doesn't really make sense, please comment and i will explain. it's pretty late where i am right now, so i may have made some typos.
also, it'd help a lot if you could specify how to do what i am supposed to do, since as you probably guessed by the fact that i can't solve such a short, simple script - i am still very new to this.
you did p:FindFirstChild("Humanoid"):TakeDamage()
and not p.Parent:FindFirstChild("Humanoid"):TakeDamage()
i've edited your code to with the necessary modifications
local canattack = true script.Parent.Blade.Touched:Connect(function(p) if canattack == true then can attack = false if p:FindFirstAncestorWhichIsA("Model") then if p.Parent:FindFirstChildWhichIsA("Humanoid") then p.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(9) wait(1) canattack = true end end end end)