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