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

Script gives error while using .Touched?

Second question of the hour, but now this isn't working.

local function ontouch(part)
					
	if part.Anchored ~= true then
		if part.Parent.Name ~= "Bombs" then
					
			wait(math.random(2,6))
			part.BrickColor = script.Parent.BrickColor
			wait(math.random(2,6))
			part:BreakJoints()
					
		end
	end
	
end

script.Parent.Touched:Connect(ontouch)

The error I get is as follows: Touched is not a valid member of Script No idea what's going on, help would be useful. Thank you.

4 comments

0

Is this script inside of another script?

0

Put the script inside a part. It should work. Because Touched events only work with an object not a script. 

0

MrFrank79 I’m going to send you a friends request on Roblox later and I’ll explain to you how that script works and what you can do to fix it.

0

The script is a first child in a cloned part

1 answer

The Touched event only works with BaseParts(e.g. MeshPart, Part, etc). Ofcourse it doesn't work on a script. A player technically can't touch a script. When using the Touched event, remember that the Instance with the event is a BasePart.

If the script is inside another script, do:

local function ontouch(part)

	if part.Anchored ~= true then
		if part.Parent.Name ~= "Bombs" then

			wait(math.random(2,6))
			part.BrickColor = script.Parent.BrickColor
			wait(math.random(2,6))
			part:BreakJoints()

		end
	end

end

script.Parent:FindFirstAncestor(PART NAME HERE).Touched:Connect(ontouch)

3 comments

0

I get the same error, i really have no idea what's happening.

0

Wait never mind, the error that showed up was: attempt to index nil with 'Touched'

0

Wait I found the solution, one of my scripts is being faulty, thank you all so much anyways.

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