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

ClickDetector doesn't kill the humanoid?

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

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)

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