I am working in a script for a weapon that does damage to NPC when the weapon touch the NPC, I want the weapon to damage every NPC that it touches one time (to damage one time per NPC).
The code make a table and insert every NPC that touch the weapon and damage him if its not in the tabel and after the attack end it reset the table, in the first attack every thing is working but after that it does not, I tryed to print the “table.Find” before the check and its working (prints that the find value is 1 and not nil) but it enter the if after that and printing “did damage” !
anyone know why ???
This is the Script :
local tool = script.Parent --the weapon
local playerCharacter = script.Parent.Parent
local ani = tool.Animations.Attack
local remoteFunction = script.Parent.RemoteFunctions.Attack --remote function or remote event
local alreadyTouched = true
local NpcList = {} --list of the NPCs that have been damaged
local function onTouched(part)
if part:IsDescendantOf(tool) or part:IsDescendantOf(playerCharacter) then return end
if alreadyTouched == false then
local firstCheck = part.Parent:FindFirstChild("Humanoid")
if firstCheck then
print("find in table value: ",table.find(NpcList, firstCheck.Parent.Name)) -- print the search value
if firstCheck:IsA("Humanoid") and table.find(NpcList, firstCheck.Parent.Name) == nil then
weaponFunc.damageNpc(firstCheck, playerCharacter.Name, id[2], 0.3)
print("did damage")
table.insert(NpcList, firstCheck.Parent.Name)
end
else
return
end
end
end
end
local function Attack()
if alreadyTouched then
alreadyTouched = false
local track = playerCharacter.Humanoid:LoadAnimation(ani)
local soundTrack = script.Parent.Sounds.slash
soundTrack:Play()
track:Play()
wait(0.6) -- track length
alreadyTouched = true
NpcList = {}
return("Attacked")
end
NpcList = {}
end
tool.SwordTop.Touched:Connect(onTouched)
remoteFunction.OnServerInvoke = Attack
Thats the output after the first attack:
In the first attack every thing is working fine there is no value in the table and it did damage one time and after that the weapon touched the NPC but did not damage because we insert the NPC name on the table
And thats the output after the second attack:
you can see in this img that it entered the if and prints “did damage”
Even though there is a value in the table.
I can not understand why !?