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
8 years ago
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)
0
Do you get any errors? viralmoose 65 — 8y
0
Nvm found the problem :D iNicklas 215 — 8y

4 answers

Log in to vote
2
Answered by 8 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!

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

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!

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


Final Product

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!

Ad
Log in to vote
2
Answered by
ImageLabel 1541 Moderation Voter
8 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.

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
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 — 8y
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 — 8y
Log in to vote
0
Answered by 8 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:

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)
2
Yeah i just realized, thank you! :D iNicklas 215 — 8y
Log in to vote
0
Answered by 8 years ago

your need to set plr as a variable

Answer this question