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

Deal Damage Only to Player?

Asked by 4 years ago

I have a basic damage script that does damage to any humanoid. I want to make it deal damage only to the player, meaning any other humanoids such as npcs would not be affected.

script.Parent.Touched:connect(function(hit) 
pcall(function() 
hit.Parent.Humanoid:TakeDamage(10)
end) 
end) 

I haven't been able to find anything like this, I don't know if it's actually possible. Does anyone have any ideas?

Thanks.

2 answers

Log in to vote
0
Answered by
Farsalis 369 Moderation Voter
4 years ago
Edited 4 years ago

What voidofdeathfire said was right and works, but there is a better way than using a generic for loop.

script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:WaitForChild("Humanoid", 3)
    local Damg = 20
    local debounce = true
    if humanoid and debounce then
        local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        if Player.Name == hit.Parent.Name then
            humanoid:TakeDamage(Damg)
            debounce = false
        end
    end
    wait(1)
    debounce = true
end)
0
Idk this seems less simple in 3 ways.... But okay voidofdeathfire 148 — 4y
0
Well yeah, my point was instead of making a for loop for utterly no reason, use a built in function. Thats all. I'll edit it to say that. Farsalis 369 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Look if the players name matches with the Humanoid's name

script.Parent.Touched:connect(function(hit) 
for i,v in pairs(game.Players:GetChildren())do
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
if humanoid.Parent.Name == v.name then
humanoid:TakeDamage(10)
end
end
end
end) 

if you have any problems please comment

Answer this question