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.