Hello, I have a script that damages Players but it also damages other NPCs which is really a problem, ive searched some answers to this problem but none of them are helpful except the Players:GetPlayerFromCharacter(parent) part but I don’t really know what to do after this.
Script:
local debounce = false
local virus = script.Parent
local function infected(virus)
local parent = virus.parent
if game.Players:GetPlayerFromCharacter(parent)then
debounce = true
while banana == true do
parent.Humanoid:TakeDamage(0.1)
wait(1)
end
end
end
virus.TouchEnded:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if Humanoid then
debounce = false
end
end)
virus.Touched:Connect(infected)
local debounce = false
local virus = script.Parent
local function infected(part) -- this parameter should be the part triggering the touched event
local parent = part.parent -- potentially a player, or an NPC, or something else
-- combined conditional that filters out everything but players
if parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(parent) then
-- do damaging stuff
end
end
virus.TouchEnded:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if Humanoid then
debounce = false
end
end)
virus.Touched:Connect(infected)
I don’t think you’ve utilized the changes I suggested.
if parent:FindFirstChild("Humanoid")
This part of the code determines if the Touched event triggerer contains a humanoid.
and game.Players:GetPlayerFromCharacter(parent)
This part of the code ensures that said humanoid belongs to an actual player. For an NPC, this value would be nil, and thus the condition does not pass.