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

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

Asked by 6 years ago
Edited 6 years ago

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
Showing us your code would be great if you want help BlackOrange3343 2676 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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().

Ad

Answer this question