Script Crashing Everything Inside

So I was looking through the forum and articles. I put this script inside my game just to use it as a note. But, it unanchores everything but the spawn. I tried placing different game scripts into Studios to see but those did not cause a crash so it just seems to be the script. Any thoughts?
*Btw the script is an event and when I play Studios it runs the event print without the character touching it and everything unanchores besides the spawn part.
*Reference page: Using Parameters and Events | Roblox Creator Documentation


local trap = script.Parent


local function onTouch(objectTouched)

	print("Something touched the trap")

	-- Destroy the touching object

	objectTouched:Destroy()

end


trap.Touched:Connect(onTouch)

1 Like

What your code does is essentially destroying everything the object is touching, so its not unanchoring anything, its just destroying it.

Edit: Can you show me where the script is located?

2 Likes

Its just a Part with a Script in it. Ohh, so I get it now, that does make sense. The example had the part on terrain instead.

So I should just change it to detect a humanoid then.

What are you trying to do? I can’t really help you if I don’t know what you are trying to achieve.

Well I was just using it as example, but essentially it was just for a humanoid. But I guess I could just change it to detect anything besides Baseplate or just detect a humanoid.

If you are trying to detect Humanoids, it won’t work. To my knowledge they only detect parts (someone go fact check me lol). If you want to detect characters, the part will detect part in the character (e.g. Head, UpperTorso etc.) which can then give you the character (using objectTouched.Parent).

Here’s the script Roblox gives as an example:



    local part = script.Parent
     
    local function onPartTouched(otherPart)
    	local partParent = otherPart.Parent
     
    	-- Look for a humanoid in the parent
    	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    	if humanoid then
    		-- Do something to the humanoid, like set its health to 0
    		humanoid.Health = 0
    	end
    end
     
    part.Touched:Connect(onPartTouched)


So what are you trying to achieve? I’m still confused.

Well it was supposed to delete the body parts touching it.

You can destroy pretty much anything.

    local part = script.Parent
     
    local function onPartTouched(otherPart)
    	local partParent = otherPart.Parent
     
    	-- Look for a humanoid in the parent
    	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
    	if humanoid then
    		otherPart:Destroy() --Destroy Touching Part
    	end
    end
     
    part.Touched:Connect(onPartTouched)

True yes, sorry I changed my script to destroy the humanoid as a global variable instead of being inside as a conditional, thats why I was confused a bit but thanks for the help there lol. Have a blessed day :slight_smile:

1 Like