Damage script problem (need help)

Can’t figure out what I did wrong (currently creating a damage script for a projectile)

Script:

function onTouch(Impact) 
if Impact.Name == "Protego" then script.Parent:remove() return end
local humanoid = Impact.Parent:FindFirstChild("Humanoid") 
if humanoid.Parent.Name == script:FindFirstChild("WandOwner").Value then script.Parent:remove() return end
if (humanoid ~= nil) then
	humanoid.Health = humanoid.Health - 15
	humanoid.PlatformStand = true

script.Parent:remove()
else
wait(.1)
script.Parent:remove()
end
end

script.Parent.Touched:connect(onTouch)

Any suggestions?

2 Likes

What’s the error?
Also, please use Preformatted text )':!

2 Likes

When the projectile hits (humanoid) there isn’t any effect D:

2 Likes

Idk if this is the issue, but it seems as though your quotation marks are curved in the script. They should be straight.

2 Likes

There’s chance that :FindFirstChild("Humanoid") will return nil. Your if statement assumes that it will return the humanoid of a player. Check for the humanoid existing first,

if humanoid and humanoid.Parent.Name == script:FindFirstChild(“WandOwner”).Value then
2 Likes

Can you use ```lua as a code block and indents so we can all read it better thanks.

2 Likes

I updated the line from

if humanoid.Parent.Name == script:FindFirstChild("WandOwner").Value then script.Parent:remove() return end

to

if humanoid and humanoid.Parent.Name == script:FindFirstChild(“WandOwner”).Value then script.Parent:remove() return end

But it doesn’t seem to work D:

2 Likes

If there is no error, maybe try changing it from a script to a local script, or vice versa.

1 Like

Nope, still not working D:

Any other suggestions?

2 Likes

Try debugging by adding print statements to see where the code reaches, like so:

function onTouch(Impact) 
	if Impact.Name == "Protego" then script.Parent:remove() return end
	local humanoid = Impact.Parent:FindFirstChild("Humanoid") 
	print("Found Humanoid")
	if humanoid.Parent.Name == script:FindFirstChild("WandOwner").Value then 
		script.Parent:remove() 
		print("WandOwner")
		return
	end
	if (humanoid ~= nil) then
		print("Hit Humanoid!")
		humanoid.Health = humanoid.Health - 15
		humanoid.PlatformStand = true
	
		script.Parent:remove()
	else
		print("Humanoid was nil")
		wait(.1)
		script.Parent:remove()
	end
end

script.Parent.Touched:connect(onTouch)

Then you can see where the code reached and trace the path of the code, which will allow you to debug easier. If you still need help, reply here with which print statements execute, and we can go from there :slight_smile:

Yeah, nothing actually happened but I got an error. The problem is i can’t figure it out. I could use some help.

" 09:39:08.853 - Touched is not a valid member of Folder

09:39:08.853 - Stack Begin

09:39:08.854 - Script ‘Players.TheDeadTed14.Backpack.Wand.WandFunction.SpellScripts.Stupefy’, Line 23

09:39:08.854 - Stack End

Players.TheDeadTed14.Backpack.Wand.WandFunction.SpellScripts.Stupefy errored!

Reason: Touched is not a valid member of Folder

Trace: Players.TheDeadTed14.Backpack.Wand.WandFunction.SpellScripts.Stupefy, line 23"

Any suggestions?

don’t use Instance:Remove(), it’s deprecated.

Use instance:Destroy() to properly leave something to be GC’ed (which it will be once all references to it are gone)

Also

they just appear that way once you format regular quotation marks, observe:
" "

"name" -- appear very slightly curved

Mine quotation marks look just the same as yours (in studios) and I have tried to use Instance:Destroy()

Here is a photo of the script.

It stills tells me that [Touched is not a valid member of Folder]!!!

Ok, this is weird. The damage script that you helped me with only works then I drag it into the dummy (with a humanoid) in run mode. But when I fire the spell in play mode with my tool nothing happens.

I am using a normal script.
‘’‘’’

I will explain a bit better. When I fire the wand the script is getting cloned and put into an effect part that has a Particle inside and the value that tells who the owner is. But the script that’s inside the effect part isn’t working.

It isn’t because it get an error that tells me [Touched is not a valid member of Folder].

Do I have to enable the script with another script?