01 | local part = script.Parent |
02 |
03 | function ontouch(plr) |
04 | if plr.Character.Name = = "Nickelele" then |
05 | part.CanCollide = false |
06 | elseif plr.Character.Name ~ = "Nickelele" then |
07 | plr.Character.Humanoid.Head:remove() |
08 | end |
09 | end |
10 |
11 | part.Touched:connect(ontouch) |
It returns a part!
1 | script.Parent.Touched:connect( function (p) |
2 | print (p) --Prints the part |
3 | end ) |
The parts parent would be the character/model/datamodel.
You need to check if the character has a humanoid!
1 | local h = part.Parent:FindFirstChild( "Humanoid" ) |
2 | if h then |
3 | print ( "Success" ) |
4 | end |
01 | local part = script.Parent |
02 |
03 | function ontouch(plr) |
04 | local h = plr.Parent:FindFirstChild( "Humanoid" ) |
05 | if h then |
06 | if plr.Character.Name = = "Nickelele" then |
07 | part.CanCollide = false |
08 | else --Don't need an elseif |
09 | h.Health = 0 --Kills |
10 | end |
11 | end |
12 | end |
13 |
14 | part.Touched:connect(ontouch) |
Hope it helps!
If your end goal is to create a "VIP" type door, I would recommend you create a listing of all users allowed to traverse said door. On contact, all you'd have to do then is check if the player that touched the door is white-listed, and then proceeding accordingly.
Also, the argument plr
is the object that touched the door, and not the corresponding player. Therefore you'd have to get the player from just that part.
Finally, you should use the :Destroy
member function of instances when to completely destroy them instead of :remove
, while simply sets their parent to nil
.
01 | local whitelist = { [ 'Nickelele' ] = true } |
02 | local door -- assign to door |
03 |
04 | local onTouch = function (hit) |
05 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 |
07 | if player then |
08 | if whitelist [ player.Name ] then |
09 | -- if the value at index `player.Name` is true then |
10 | door.CanCollide = false -- execute |
11 | else |
12 | hit.Parent:BreakJoints() -- break all joints |
13 | -- associated with hit.Parent, or Character |
14 | end |
15 | end |
16 | end |
Your Script had two issues, first your trying to characterize a part, and secondly your trying to get head out of humanoid. so here's the fixed code:
01 | local part = script.Parent |
02 |
03 | function ontouch(plr) |
04 | if plr.Parent.Name = = "Nickelele" then |
05 | part.CanCollide = false |
06 | elseif plr.Parent.Name ~ = "Nickelele" then |
07 | plr.Parent.Head:remove() |
08 | end |
09 | end |
10 |
11 | part.Touched:connect(ontouch) |