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

Script that kills me me when i touch a part kills me when i'm not touching it?

part = game.Workspace.Part

game.Workspace:findFirstChild("ReportingDosntWork") part.Position = Vector3.new(-6,1000,-11)

function onTouched(part) game.Workspace.ReportingDosntWork.Humaniod.Health = 0 end

part.Touched:Connect(onTouched)

When I use this script, it kills me even when i don't touch it. Can somebody help?

1 comment

0

You need to make a script to check if player touch it

3 answers

✓ Accepted answer

For your script, whenever part is touched it kills you. That means if it's touched by another part it will kill you, for exmaple if it touches the baseplate, you're a dead boi

part = game.Workspace.Part

function onTouched(part)
	if part.Parent.Name == 'ReportingDosntWork' then
		part.Parent.Humanoid.Health = 0
	end
end

part.Touched:Connect(onTouched)

2 comments

0

Put this in your part

0

This is not a good way to check if the object which touched is the character - use :GetPlayerFromCharacter(part.Parent) instead.

While Icy_Qube's solution works in most cases, but in some circumstances it doesn't work.


Why it doesn't work


A general issue with checking if part.Parent.Name == "ReportingDosntWork" is that it doesn't know if it is a player or an NPC, or anything else that is named "ReportingDosntWork".


More efficient method


So, to make this work efficiently, you need to use the :GetPlayerFromCharacter function, which gets a player object from a character. The reason we use this is that all body parts are descendants of the character.


Finished Product


part = game.Workspace.Part

function onTouched(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		hit.Parent.Humanoid.Health = 0
	end
end

part.Touched:Connect(onTouched)

1 comment

0

you can also use the :TakeDamage function of the humanoid

Put this in the part you wish


local Brick = script.Parent


local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = 0
	end
end

Brick.Touched:connect(PlayerTouched)

You can also change how much damage you want

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