Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

This part damages players but not npcs?

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

3 comments

0

You are checking on line 9 if the model belongs to a player. Perhaps remove that check.

0

That worked! Thanks.

0

true, it is the answer

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.