Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

This part damages players but not npcs?

Asked by
exarlus 72
5 years ago

Whenever a player touches a part that the script is parented to the player would take damage. But for some reason npcs do not take damage from it. How would I fix this?

local Players = game:GetService("Players")
local Debounce = .3
local Damage = 5
while wait(Debounce) do
    local Connection = script.Parent.Touched:Connect(function() end)
    for _,Object in pairs(script.Parent:GetTouchingParts()) do
        Connection:Disconnect()
        local Rig = Object:FindFirstAncestorWhichIsA("Model")
        if Rig ~= nil and Players:GetPlayerFromCharacter(Rig) ~= nil then
            local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
            if Humanoid ~= nil then
                Humanoid:TakeDamage(Damage)
            end
        end
    end
end
0
You are checking on line 9 if the model belongs to a player. Perhaps remove that check. Vathriel 510 — 5y
0
That worked! Thanks. exarlus 72 — 5y
0
true, it is the answer gloveshun 119 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
local Players = game:GetService("Players")
local Debounce = .3
local Damage = 5
while wait(Debounce) do
    local Connection = script.Parent.Touched:Connect(function() end)
    for _,Object in pairs(script.Parent:GetTouchingParts()) do
        Connection:Disconnect()
        local Rig = Object:FindFirstAncestorWhichIsA("Model")
        if Rig ~= nil then
            local Humanoid = Rig:FindFirstChildOfClass("Humanoid")
            if Humanoid ~= nil then
                Humanoid:TakeDamage(Damage)
            end
        end
    end
end
Ad

Answer this question