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

OnTouch script help?

Asked by
iNicklas 215 Moderation Voter
9 years ago
01local part = script.Parent
02 
03function 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
09end
10 
11part.Touched:connect(ontouch)
0
Do you get any errors? viralmoose 65 — 9y
0
Nvm found the problem :D iNicklas 215 — 9y

4 answers

Log in to vote
2
Answered by 9 years ago

Problems

  • Touched returns a part, not a player
  • You should use Part.Parent
  • You should see if Part.Parent is actually a player
  • Use destroy not remove. Remove is deprecated.

Touched

It returns a part!

1script.Parent.Touched:connect(function(p)
2    print(p) --Prints the part
3end)

Part's Parent

The parts parent would be the character/model/datamodel.


See if the model is a character

You need to check if the character has a humanoid!

1local h = part.Parent:FindFirstChild("Humanoid")
2if h then
3    print("Success")
4end


Final Product

01local part = script.Parent
02 
03function 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
12end
13 
14part.Touched:connect(ontouch)



Hope it helps!

Ad
Log in to vote
2
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

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.

01local whitelist = {['Nickelele'] = true}
02local door -- assign to door
03 
04local 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
16end
0
The way you did your white list you could use inpairs and you can get the name and if it's true or false. EzraNehemiah_TF2 3552 — 9y
0
There's no need for a long iterations when you can simply just index the player's Name. The pairs iterator would work, but isn't a necessity.  ImageLabel 1541 — 9y
Log in to vote
0
Answered by 9 years ago

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:

01local part = script.Parent
02 
03function 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
09end
10 
11part.Touched:connect(ontouch)
2
Yeah i just realized, thank you! :D iNicklas 215 — 9y
Log in to vote
0
Answered by 9 years ago

your need to set plr as a variable

Answer this question