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

How to kill player when you click a part?

Asked by 4 years ago
-- I tried this.
local cursor = workspace.Part.ClickDetector
local part = script.Parent
local human = script.Parent:FindFirstChild("Humanoid")
function onClick()
    if human then
        human.Health = 0
    end
end
cursor.MouseClick:Connect(onClick)
-- I added a clickDetector

2 answers

Log in to vote
0
Answered by
Subsz 366 Moderation Voter
4 years ago

you are trying to define the Humanoid inside the part, not the player that clicked it.

Here's how you fix that :

local cursor = workspace.Part.ClickDetector

function onClick(player)
local human = player.Character.Humanoid
    if human then
        human.Health = 0
    end
end

cursor.MouseClick:Connect(onClick)
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
local clickDetector = script.Parent

local function onClick(playerClicked)
    local character = playerClicked.Character
    if (character) then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if (humanoid) then
            humanoid:TakeDamage(humanoid.Health)
        end
    end
end

clickDetector.MouseClick:Connect(onClick)

Answer this question