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

ClickDetector doesn't kill the humanoid?

Asked by 3 years ago
local clickdetector = workspace.Part.ClickDetector

local function onMouseClick(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid.Health = 0
    end
end

clickdetector.MouseClick:connect(onMouseClick)

I want the code to set humanoid's health to 0 when clickdetector is fired but nothing happens. Any fix?

1 answer

Log in to vote
0
Answered by
mroaan 95
3 years ago
Edited 3 years ago

my dude MouseClick events gives you the player not the "otherPart" so to get the player's character make a variable inside the function like this:

local clickdetector = workspace.Part.ClickDetector

local function onMouseClick(player)
    local char = player.Character or player.CharacterAdded:Wait() -- character of the player
    local humanoid = char:FindFirstChild("Humanoid") -- the humanoid
    if humanoid then
        humanoid.Health = 0
    end
end

clickdetector.MouseClick:connect(onMouseClick)
Ad

Answer this question