I’ve made a simple sword tool that when activated will damage any hostile NPCs and not other players. The problem is when I tested it with a roblox rig, it doesn’t take any damage. But when I tested it with my own character that I imported with a plugin, it does take damage. I heard some people having this issue as well, so what exactly causes this?
provide the script so we may locate the issue?
Your sword tool probably looks for a humanoid with a unique name so you exclude players, basically you don’t look for “Humanoid” but instead “NPC”.
local Character
local Player
local ScriptFolder = script.Parent
local Tool = ScriptFolder.Parent
local AnimFolder = Tool:WaitForChild("Animations")
local IdleAnim = AnimFolder:WaitForChild("TestSword_Idle")
local Swing1Anim = AnimFolder:WaitForChild("TestSword_Swing1")
local IdleTrack
local Swing1Track
local Cooldown = false
local CooldownWait = 1
Tool.Equipped:Connect(function()
Character = Tool.Parent
Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
IdleTrack = Humanoid:LoadAnimation(IdleAnim)
Swing1Track = Humanoid:LoadAnimation(Swing1Anim)
end
IdleTrack:Play()
end)
local function Attack()
if Cooldown == false then
Swing1Track:Play()
print("Attacked")
Cooldown = true
print("Cooldown")
wait(CooldownWait)
Cooldown = false
print("Cooldown done")
end
end
Tool.Activated:Connect(Attack)
Tool.Handle.Touched:Connect(function(hit)
if hit.Parent.Parent:GetAttribute("IsHostile") == true and Cooldown == true then
hit.Parent.Parent:WaitForChild("Humanoid").Health -=10
print("hit "..hit.Name)
end
end)
Tool.Unequipped:Connect(function()
IdleTrack:Stop()
end)
pretty sure the problem is around these lines: (47-48)
if hit.Parent.Parent:GetAttribute("IsHostile") == true and Cooldown == true then
hit.Parent.Parent:WaitForChild("Humanoid").Health -=10
It probably doesn’t attack the roblox rig because it doesn’t have a “IsHostile” attribute.
Both the rig (NPC) and my character npc (AnceshNPC) have the “IsHostile” attribute
Do you get any errors?
(character limit)
Nope no error at all, just print outputs
Maybe try changing :WaitForChild("Humanoid")
to :FindFirstChildOfClass("Humanoid")
?
put a print in between the lines and show me what it outputs
That doesn’t work either.
I don’t know why but when I put another character npc with the same plugin, It will only kill my own character and not the other npcs. Does this have something related with the player that uses the sword?
Try
if hit:FindFirstAncestorOfClass("Model"):GetAttribute("IsHostile") --== true
That doesn’t work either
(char)
Okay so I “might” have fixed this problem (?) but I’m not entirely sure what actually happened here.
The way I fix this problem is by inserting a character model (can be any player) with a plugin. And the character’s HumanoidRootPart
SHOULD be unanchored otherwise it wouldn’t work.
For some reason roblox’s rig just can’t be damaged with my sword script. I have tested it with the classic roblox sword and other weapons and it can damage the rig. I’m not sure what causes this because it doesn’t show any errors whatsoever.
My current script
Tool.Handle.Touched:Connect(function(hit)
if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
return
end
if hit.Parent.Parent:GetAttribute("IsHostile") and Cooldown == true then
wait(.1)
hit.Parent.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(10)
print("hit "..hit.Parent.Parent.Name)
end
end)
I will not mark this as a solution yet just in case somebody can figure out the problem
its hit.Parent.Parent instead of Hit.Parent