Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

OnTouch script help?

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)

2 comments

0

Do you get any errors?

0

Nvm found the problem :D

4 answers

✓ Accepted answer

#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!

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

2 comments

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.

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. 

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)

1 comment

2

Yeah i just realized, thank you! :D

This discussion is preserved as it was. New answers, comments, and votes are closed.