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

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

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

✓ Accepted answer

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)

2 comments

0

oh my lord, i'm such an idiot... thank you so much <3

0

lol, happy to help

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