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

How to make a script that tests if a player is touching an object with a certain name?

I am making a sandbox game where you can make your own obby. I am trying to make a lava script, but when I put in a script with the touched property under an object it affects the game in a way that i don't want. So i thought of making a player or character script that tests if a player touches a Model or part with the name of "Lava." But sadly, I don't know how to do this.

I put this script in StarterCharacterScripts:

player = script.Parent

while true do
	
for i,v in pairs(player:GetDescendants()) do
	v.Touched:connect(function(part)
		if part.Name == MainPart and part.Parent == Lava then
			player:Destroy()
		end
	end)
end


end

1 comment

1

Showing us your code would be great if you want help

1 answer

✓ Accepted answer

So um, there are some problems with your script :

  • Not everything in the player’s character has a .Touched event, so you’re going to get some errors which will break the whole script. You should add a conditional statement like “if part:IsA('BasePart') then”.

  • Using a while true do loop here is probably inefficient and unnecessary. When the script runs, it’ll set up all the functions and they will run once the character touches something. You don’t need a loop to spam connect the events.

  • You need to add "" or '' on “MainPart” and “Lava” as they are strings. Not adding them will give you an error.

  • I don’t know if this is necessary or not but put “part.Parent.Name” instead of “part.Parent”. I think doing “part.Parent” only will get the object but not its name. If it works with “part.Parent” though just ignore this.

  • Also use :Connect(). :connect() still works but it’s deprecated so it’s recommended to use :Connect().

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