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

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

Asked by 6 years ago
Edited 6 years ago

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?

0
You need to make a script to check if player touch it brodski40 8 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
Put this in your part FantasticFrontierGuy 33 — 6y
0
This is not a good way to check if the object which touched is the character - use :GetPlayerFromCharacter(part.Parent) instead. SummerEquinox 643 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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)
0
you can also use the :TakeDamage function of the humanoid theking48989987 2147 — 6y
Log in to vote
0
Answered by 6 years ago

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

Answer this question