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

How do i get the whole character?

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

✓ Accepted answer

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)
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...

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