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
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
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
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 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.