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

How do i get the whole character?

Asked by
PropzFx 27
5 years ago
Edited 5 years ago

Im trying to make a script so when a character gets touched by a laser it will die. Im able to make so when the laser touches some parts in the character but i need the whole character cuz otherwise they wont die when the laser touches those parts. Heres the script: (I dont get any errors cuz this script works fine but not as i want it to)

laser = script.Parent

laser.Touched:Connect(function(part)
    for i,v in pairs(game.Players:GetPlayers()) do
        if part.Parent.Name == "Humanoid" then
            part.Parent.Health = 0
        elseif part.Name == "Head" then
            part.Parent.Humanoid.Health = 0
        elseif part.Parent == v.Name then
            part.Parent.Humanoid.Health = 0
        end
    end


end)

2 answers

Log in to vote
0
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
5 years ago

What you can do is use GetPlayerFromCharacter to get the character of the player that was it from the part that was hit.

laser.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        player.Character.Humanoid.Health = 0
    end
end)
Ad
Log in to vote
0
Answered by 5 years ago
laser = script.Parent

laser.Touched:Connect(function(part)
    local char = part.Parent
    if char then
        local h = char:FindFirstChildOfClass("Humanoid")
        if not h then
            h = char.Parent:FindFIrstChildOfClass("Humanoid")
        end

        if h then
            h.Health = 0
        end
    end
end)

You don't need to iterate through the players...

Answer this question