local part = script.Parent function ontouch(plr) if plr.Character.Name == "Nickelele" then part.CanCollide = false elseif plr.Character.Name ~= "Nickelele" then plr.Character.Humanoid.Head:remove() end end part.Touched:connect(ontouch)
It returns a part!
script.Parent.Touched:connect(function(p) print(p) --Prints the part end)
The parts parent would be the character/model/datamodel.
You need to check if the character has a humanoid!
local h = part.Parent:FindFirstChild("Humanoid") if h then print("Success") end
local part = script.Parent function ontouch(plr) local h = plr.Parent:FindFirstChild("Humanoid") if h then if plr.Character.Name == "Nickelele" then part.CanCollide = false else --Don't need an elseif h.Health = 0 --Kills end end end 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
.
local whitelist = {['Nickelele'] = true} local door -- assign to door local onTouch = function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then if whitelist[player.Name] then -- if the value at index `player.Name` is true then door.CanCollide = false -- execute else hit.Parent:BreakJoints() -- break all joints -- associated with hit.Parent, or Character end end 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:
local part = script.Parent function ontouch(plr) if plr.Parent.Name == "Nickelele" then part.CanCollide = false elseif plr.Parent.Name ~= "Nickelele" then plr.Parent.Head:remove() end end part.Touched:connect(ontouch)