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

TakeDamage renders an error, how do i fix my script?

Asked by 5 years ago
Edited 5 years ago

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.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
oh my lord, i'm such an idiot... thank you so much <3 User#28022 0 — 5y
0
lol, happy to help User#23252 26 — 4y
Ad

Answer this question