How would I fix this kill script?

What I’m trying to achieve is a functional kill script that can detect a model with a humanoid and damage it.

I’m experiencing some weird issue that I can’t resolve with the kill script. (More below the listed script)

Currently, I’ve tried switching it around, but it always kills the player that isn’t supposed to be killed when switched around.

The script will be listed below:

local Knockback = script.Knockback
local Min = script.Minimum
local Max = script.Maximum
local Owner = script.Owner
local Force = 0
local PreDistance = nil

local function Run()
while wait(1) do
if PreDistance == nil then
PreDistance = script.Parent.Position
end
Force = (script.Parent.Position - PreDistance).magnitude
PreDistance = script.Parent.Position
end
end
local NewThread = coroutine.wrap(Run)
NewThread()

function Touch(Part)
local P = Part.Parent
if P.ClassName == “Model” and P~= game.Workspace then
if P:FindFirstChildOfClass(“Humanoid”) or P:FindFirstChild(“Humanoid”) then
if Part.Parent.Name == Owner.Value or P.Name == script.Owner.Value then
print(“The owner of the part cannot be killed!”)
else
print(P.Name)
local Hum = P:FindFirstChildOfClass(“Humanoid”)
local Damage = math.random(Min.Value,Max.Value)
Hum.Health = Hum.Health - Damage
if Knockback.Value > 0 then
if P:FindFirstChild(“HumanoidRootPart”) then
P.HumanoidRootPart.Velocity = script.Parent.CFrame.LookVector * (Knockback.Value * Force)
end
end
end
end
end
end
script.Parent.Touched:Connect(Touch)

The problems I’m having with the script happen in the Touch function, but for some reason it’s not producing any error in the developer console and I can’t seem to figure out if I made a scripting mistake or the script just doesn’t work at all. (It should work, as I’ve made it print various texts at certain times throughout the script.) Whenever the script reaches the part that detects if the Part’s parent is the player’s character or the owner of the part, it stops working and doesn’t continue. All the values that are a child of the Part are always correct, so it couldn’t possibly be that.

Hi.
I’ve noticed a few things.
First thing I noticed is that you are not using the ``` format for your code.
Another thing I noticed is that you aren’t using the best method of detecting if a part is apart of a player.
Try doing this instead:

function Touched(Part)
    if Part:FindFirstAncestorOfClass("Model") then
        local Parent = Part:FindFirstAncestorOfClass("Model")
        local Hum = Parent:FindFirstChild("Humanoid")
        if Hum then
            --do a thing
        end
    end
end

This may resolve an issue.
Also, you should use ObjectValues (for things like your owner value)
(also try using humanoid:TakeDamage() instead of subtracting the health.)

I’ve tried multiple methods of trying to fix the kill script. I’ve tried your method and it seems to not work, and I’ve tried a method that I know works, and still doesn’t work. I checked to make sure all values are correct, and they appear to be correct.

Would you please let us see the latest version of your script, correctly formatted with ``` so we can help you further. If you had prints in the script when run would you also let us know what they showed.

I managed to fix it by moving the script into another place. I think the place I was in broke the script, even one I knew worked was broken in the place.