You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make it so that my damaged script can’t kill the NPC that it is parented to. For some reason, when the grab animation plays when others touch the hitbox, there’s a high chance the damage script kills the NPC within seconds. EVEN WHEN I PUT A DEBOUNCE ON IT.
Currently, this is the script that damages:
local hit = 0
script.Parent.Touched:Connect(function(touched)
local human = touched.Parent:FindFirstChild("Humanoid")
if human ~= nil and script.Parent.Parent.Humanoid.Health ~= 0 and touched.Parent.Name ~= script.Parent.Parent.Name and touched.Parent.Parent.Name ~= script.Parent.Parent.Name then
if hit == 0 then
human:TakeDamage(80)
hit = 1
wait(2)
hit = 0
end
end
end)
And this is the script that grabs and plays the animation:
local Part = script.Parent
local Anim = script.Grab
local AnimPlay = script.Parent.Parent.Humanoid.Animator:LoadAnimation(Anim)
local Script = script.Parent.Parent.AttackPeople
Part.Touched:Connect(function(otherPart)
local NewWeld = Instance.new("Weld")
local Check = otherPart.Parent:FindFirstChild("HumanoidRootPart")
local WeldCheck = script.Parent:FindFirstChild("Weld")
if Check ~= nil and otherPart.Parent.Name ~= script.Parent.Parent.Name and not WeldCheck and otherPart.Parent.Parent.Name ~= script.Parent.Parent.Name then
NewWeld.Parent = Part
NewWeld.Part0 = Part
NewWeld.Part1 = Check
Script.Disabled = true
AnimPlay:Play()
wait(1.5)
for _,v in pairs(script.Parent:GetChildren()) do
if v:IsA("Weld") then
v:Destroy()
end
end
AnimPlay.Stopped:Connect(function()
Script.Disabled = false
end)
end
end)
And here is the setup:
Note: no other scripts have to do with dealing damage.
spawn(function()
while true do
local touched = script.Parent.Touched:Wait()
if touched then
local human = touched.Parent:FindFirstChild("Humanoid")
if human ~= nil and script.Parent.Parent.Humanoid.Health ~= 0 and touched.Parent.Name ~= script.Parent.Parent.Name and touched.Parent.Parent.Name ~= script.Parent.Parent.Name then
human:TakeDamage(80)
wait(2)
end
end
end
end)
Basically, this will make all your touch events in one thread. When the touch event activates, the script needs to wait for all the lines of code following to run before listening to the touch event again. You can use the same concept for your other script. But since I never tested this before, I’m not sure if it works. Tell me if it does!
Yep. The code is a mess though, I fixed it up for you:
local hit = false; -- Don't use a number, it uses more memory.
--// Make variables to prevent repititive code
local myChar = script:FindFirstAncestorOfClass("Model")
local myHumanoid = myChar:WaitForChild("Humanoid")
local TouchedEvent = script.Parent.Touched:Connect(function(touched)
local char = touched:FindFirstAncestorOfClass("Model")
local human = char and char:FindFirstChild("Humanoid")
if not hit human and char.Name ~= myChar.Name then
human:TakeDamage(80)
hit = true
wait(2)
hit = false
end
end)
myHumanoid.Died:Connect(function()
TouchedEvent:Disconnect() -- Stop firing the event once the humanoid died
end)
local hit = false; -- Don't use a number, it uses more memory.
--// Make variables to prevent repititive code
local myChar = script:FindFirstAncestorOfClass("Model")
local myHumanoid = myChar:WaitForChild("Humanoid")
local TouchedEvent = script.Parent.Touched:Connect(function(touched)
local char = touched:FindFirstAncestorOfClass("Model")
local human = char and char:FindFirstChild("Humanoid")
if human and char.Name ~= myChar.Name then
human:TakeDamage(80)
hit = true
wait(2)
hit = false
end
end)
myHumanoid.Died:Connect(function()
TouchedEvent:Disconnect() -- Stop firing the event once the humanoid died
end)
If the problem is the NPC damages itself, try using If Statements to check if the humanoid’s parent has the same name as the NPC before it does the damage.