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

How do I fix the problem with the Touched function?

This simply just doesn't print, no error.

script.Parent.Touched:connect(function(hit)
	if hit.Name ~= "FrontDropBrick" or hit.Transparency ~= 1 then
		print(hit.Name)
	end
end)

1 comment

0

If you're checking both values, use and instead of or. Also, you should do an else statement to see if it even works. If not, check to see if it's disabled.

2 answers

If a script doesn't work and gives no errors, adding prints is your friend. After you have checked that the script is in fact not disabled (in the properties), and that it's inside the part you want it to detect touches from, try adding prints. First, print before the function to see if the script itself runs, then print within the function to see if it's correctly fired, and then print to see if the hit has the properties it should to work.

print("Script has run!")
script.Parent.Touched:connect(function(hit)
	print("Part has been touched...")
	print("Part name is: " .. hit.Name .. ", passes first statement: ".. tostring(hit.Name ~= "FrontDropBrick"))
	print("Part transparency is: " .. hit.Transparency .. ", passes second statement: ".. tostring(hit.Transparency ~= 1))
    if hit.Name ~= "FrontDropBrick" or hit.Transparency ~= 1 then
        print(hit.Name)
    end
end)

If either prints say "false" after the "passes statement", it means that the part did not have one of the required properties to pass. If this is the case, try switching the equals to solve this (remembering == means 'is equal to', and ~= means 'does not equal to'.

Hope this helps!

##Why Your Script Doesn't Work

Your script might not work because one, maybe both of the conditions are not true. In an if-statement, your condition has to be true. You used or, so, one of the conditions has to be true. To make sure the conditions are not true, we could use an else statement to print some text

##How To Check Hit

It is good practice to check if the parent of hit isn't nil. You simply wrap an if statement on top or use and

##Don't Use Deprecated Syntaxes(functions etc.)

I have noticed that you have used :connect() instead of :Connect(). :connect() is deprecated, or not recommended to use.

Here is the final script

script.Parent.Touched:Connect(function(hit)
      if hit.Name ~= "FrontDropBrick" or hit.Transparency ~= 1  and hit.Parent then
            print(hit.Name)
      else
	    print("hit is not named 'FrontDropBrick' or hit transparency is not 1 or hit.Parent is nil")
      end
end)

If there are any questions, feel free to post them in the comments

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