I got a sword script off of youtube and modified it but it doesn't work?
01 | if script.Parent.Blade.Touched = = game.Workspace.Enemies.Dark 4 then |
02 |
03 | if script.Parent.CanDamage.Value = = true then |
04 |
05 | script.Parent.CanDamage.Value = false |
06 |
07 | game.Workspace.Enemies.Dark 4. Zombie.Health = game.Workspace.Enemies.Dark 4. Zombie.Health - 100 |
08 | else |
09 | print ( "no" ) |
10 | end |
11 |
12 | end |
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.
01 | local enemies = game.Workspace.Enemies:GetChildren() --// All the children of the enemies folder. |
02 |
03 |
04 | for i, v in pairs (enemies) do |
05 | if v:IsA( "Model" ) then --// If the child is a model |
06 | if v:FindFirstChild( "Humanoid" ) then --// If the model has a humanoid |
07 | wait( 1 ) |
08 | v.HumanoidRootPart.Touched:Connect( function () --// When the sword touches the npc |
09 | v.Humanoid:TakeDamage( 10 ) --// Npc takes 10 Damage |
10 | wait( 1 ) |
11 | end ) |
12 | end |
13 | end |
14 | end |
OR you can use this one that only will damage when you click with the tool!
01 | local enemies = game.Workspace.Enemies:GetChildren() --// All the children of the enemies folder. |
02 |
03 |
04 | for i, v in pairs (enemies) do |
05 | if v:IsA( "Model" ) then --// If the child is a model |
06 | if v:FindFirstChild( "Humanoid" ) then --// If the model has a humanoid |
07 | wait( 1 ) |
08 | v.HumanoidRootPart.Touched:Connect( function () --// When the sword touches the npc |
09 | script.Parent.Activated:Connect( function () --// If you click holding the sword it will damage. |
10 | v.Humanoid:TakeDamage( 10 ) --// Npc takes 10 Damage |
11 | wait( 1 ) |
12 | end ) |
13 | end ) |
14 | end |
15 | end |
16 | end |
Instead of doing all this extra, you need to target what the sword is hitting instead of referencing the workspace. For example:
01 | local sword = script.Parent |
02 | local dmg = #ofdamageuwantswordtodo |
03 |
04 | sword.Touched:Connect( function (hit) |
05 | if hit.Parent:FindFirstChild( 'Dark4' ) then |
06 | humanoid = hit.Parent:FindFirstChild( 'Dark4' ).Humanoid |
07 | humanoid:TakeDamage(dmg) |
08 |
09 | end |
10 | end ) |
ok now you got a solution. put this inside the sword tool
1 | script.Parent.Activated:Connect( function () |
2 | script.Parent.Handle.Touched:Connect( function (hit) |
3 | if hit.Parent.Name = = script.Parent.Parent.Name then |
4 | else hit.Parent.Humanoid.Health = 0 |
5 | end |
6 | end ) |
7 | end ) |