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

My sword script is not doing damage to enemies?

I got a sword script off of youtube and modified it but it doesn't work?



if script.Parent.Blade.Touched == game.Workspace.Enemies.Dark4 then

if script.Parent.CanDamage.Value == true then

script.Parent.CanDamage.Value = false

game.Workspace.Enemies.Dark4.Zombie.Health = game.Workspace.Enemies.Dark4.Zombie.Health - 100
else
	print("no")
end

end

5 comments

0

for game.Workspace.Enemies.Dark4.Zombie.Health - 100 you did this - instead of this =

0

that was just a test, I am wanting to take like 20 health off of it

0

It's not your sword script, it's a random person's sword script. We don't help with free code.

0

but it is mine? Line 2 and 3 are theirs the rest is mine

0

Line 2 is a blank line and it wouldn't make sense for 3 to be made by you but the rest not because on line 5 it does something which supports the functionality line 3 gives.

3 answers

✓ Accepted answer

Well, in this case for iv loop is the best, so the script I made here, you will not need to make new scripts, just get an npc and put it in the enemies folder, it will automatically take damage if the sword touches the npc. Btw this script should be in the tool and im assuming you have a part named Blade.

local enemies = game.Workspace.Enemies:GetChildren() --// All the children of the enemies folder.


for i, v in pairs(enemies) do
	if v:IsA("Model") then --// If the child is a model
		if v:FindFirstChild("Humanoid") then --// If the model has a humanoid
			wait(1)
			v.HumanoidRootPart.Touched:Connect(function() --// When the sword touches the npc
				v.Humanoid:TakeDamage(10) --// Npc takes 10 Damage
				wait(1)
			end)
		end
	end
	end

OR you can use this one that only will damage when you click with the tool!

local enemies = game.Workspace.Enemies:GetChildren() --// All the children of the enemies folder.


for i, v in pairs(enemies) do
	if v:IsA("Model") then --// If the child is a model
		if v:FindFirstChild("Humanoid") then --// If the model has a humanoid
			wait(1)
			v.HumanoidRootPart.Touched:Connect(function()--// When the sword touches the npc
				script.Parent.Activated:Connect(function() --// If you click holding the sword it will damage.
				v.Humanoid:TakeDamage(10) --// Npc takes 10 Damage
				wait(1)
			end)
			end)
			end
	end
	end

2 comments

0

When I hit my enemy it doesn't do damage still. I know it isn't the enemy because with the classic sword it works, I would use the classic sword but it uses a mesh and I can't make meshes

0

nvm I needed the rename something, thanks!

Instead of doing all this extra, you need to target what the sword is hitting instead of referencing the workspace. For example:

local sword = script.Parent
local dmg = #ofdamageuwantswordtodo

sword.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Dark4') then
		humanoid = hit.Parent:FindFirstChild('Dark4').Humanoid
			humanoid:TakeDamage(dmg)

		end
	end)

10 comments

0

You also need to make sure whatever you're hitting, I'm assuming a zombie... has the humanoid property in it.

0

thanks, if the humanoid property is named Zombie do I change Humanoid to Zombie?

0

Yes, and if there is a child in the Zombie Model named "Dark4" then keep it, but if it's not change both of them. If this works, please make sure to hit the button that lets everyone know I've answered your question.

0

It still doesn't do damage though?

0

I tested the classic sword and that does do damage, I tried to move my parts into the classic sword but my sword got flipped when I held it

0

My script works perfectly, if it's not working that's a physical issue.. probably welding. Don't know why my answer was removed.

0

It doesn't do damage to my entity or players

0

Take a block, a part and insert the script I just said into it, then replace "Dark4" with Humanoid, remove ".Humanoid" and put dmg to 1. You will see that it works, then make my answer correct again!

0

Yea, but it doesn't do any damage to my enemies! I am not trying to kill a block, also it isn't the enemy because the classic sword worked!

ok now you got a solution. put this inside the sword tool

script.Parent.Activated:Connect(function()
script.Parent.Handle.Touched:Connect(function(hit)
if hit.Parent.Name == script.Parent.Parent.Name then
else hit.Parent.Humanoid.Health = 0
end
end)
end)

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