Print() deciding if my code works or not

Today I am getting a very confusing “invisible” error. My output logs are giving me no error, yet I do think my code is running.

  1. What do you want to achieve? Keep it simple and clear!
    I am using CollectionService to tag “pellets” and run code when they are touched. When a testing print() (print("RUNNING")) is not commented out, my code works. When it is commented out, however, it does work? Why is this?
  2. What is the issue?
    Here is my code:
for _, TaggedPart2 in pairs(TaggedParts2) do
	TaggedPart2.Touched:Connect(function(hit)
		--print("RUNNING?")
		if hit.Name == "brainCell" or hit.Name == "mainBCell" or hit.Name == "skinCell" or hit.Name == "toothCell" then
			print("RUNNING")
			if string.match(hit.Parent.Name, "_cells") ~= nil then
				local hID = string.gsub(hit.Parent.Name, "_cells", "")
				local player = Players:GetPlayerByUserId(hID)
				local leaderstats = player:WaitForChild("leaderstats")
				local points = leaderstats:FindFirstChild("Points")
				points.Value = points.Value + 1
			end
		genPellet()
		TaggedPart2:Destroy()
		end
	end)
end

If I switch the line to: --print("RUNNING")
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
This completely confuses me, as I am getting no errors. I have checked the hub, but to no avail. To clarify what the script above does: if hit is a “cell”, delete part and give the player a point. It does work when the print() is not commented, but it doesn’t otherwise. What is the issue here?

Also, I tried to add if TaggedPart2:CanCollideWith(hit) then, but it also broke the code.

Any and all help are much appreciated!
Someperson576

P.S. If I can get Gyazo to work, I’ll add videos.

Edit: Some additional information here: This is the second collection service loop in the specific script. If the first isn’t working, could that affect this?

2 Likes

To summarize what it looks like when it works, the player’s part hits a pellet and the pellet disappears. Then the player gains a point. Without the print() line, the player’s part can knock the pellet over and nothing happens.

1 Like

Most likely, it is due to the fact that TaggedPart2 is touching so many other parts and therefore it’s prints so much that it will not work.

1 Like

It’s basically timing out the script.

If you’re using cframe, this won’t work. I think when parts are cframed to touch, the touched event doesn’t fire. You could use a body position I think or another method but I forgot the other one. I just wrote this to tell you the problem, I forgot the solution.

If the touched event does fire and that’s not your problem, the problem might be your if statement. I’m guessing the commented print works but the one in the if statement doesn’t. If that’s the case you can check if it’s the if statement by printing out the conditions in it:

print(hit.Name == "brainCell" or hit.Name == "mainBCell" or hit.Name == "skinCell" or hit.Name == "toothCell")

If it prints false then the script isn’t working because the if statement is false.

1 Like

Prints should not be the determining factor of if your code functions or not. Are you sure it’s not functioning with the print not commented out?

@sasial and everyone else, the way the current situation works is that I have 1k
“Pellets” that are generated. Every pellet is on a flat part, kind of like a smooth, invisible baseplate. Then a player’s part comes along. When the print() is there, everything works fine. However, this appears to be very finicky. What times out the script, and how can it not time it out?

@Criminimin, with the print() enabled, the end result works (performs as it should). Without print() enabled, the tagged part is not deleted, and points are not added. This leads me to believe it I should a timed out thing.

Edit: I also tried the can collide thing, and it broke even with the print(). Still leads me to timed out.

@iNoobe_yt, I am not cframing. Also, that if statement is like that because it was the only thing I got to work. I tried checking for cellValue, all of the “cell” parts have one, but it didn’t work either. Still leads me to timing out. But why would it be timing out?

Can you print out the if statements to see whether it returns true/false for all the if statements you have? You could also debug your script by using a debugger or by printing out random things to see when the print doesn’t work. If one of the prints you have placed doesn’t fire then the script errored on one of the lines prior to the print.

for _, TaggedPart2 in pairs(TaggedParts2) do
	TaggedPart2.Touched:Connect(function(hit)
		print("Touched fired")
		if hit.Name == "brainCell" or hit.Name == "mainBCell" or hit.Name == "skinCell" or hit.Name == "toothCell" then
			print("First if statement passed")
			if string.match(hit.Parent.Name, "_cells") ~= nil then
				print("Second if statement passed")
				local hID = string.gsub(hit.Parent.Name, "_cells", "")
				local player = Players:GetPlayerByUserId(hID)
				local leaderstats = player:WaitForChild("leaderstats")
				local points = leaderstats:FindFirstChild("Points")
				points.Value = points.Value + 1
			end
			genPellet()
			TaggedPart2:Destroy()
		end
	end)
end

Here is your script with the prints. Please run this to find the problem in your script.

This is still very odd. Sometimes it runs, other times it doesn’t. I tried three times, and it only worked once. Here’s the output log:

Ignore the other error, I am working on a gui at the moment, and am changing the link. Anyways, it works as expected with this.

Ok, a quick replay. 1 k parts are generated. I also have this other part that is moving around and testing. It passed the first if statement, so the part was destroyed and another generated. Touched fired (x2) is the next one generated and the next one eaten. How odd!

Sounds like the for loop is confused whether you’re calling the parameter inside pairs or the value.

How would I fix that?(30chars)

No, I just realised that the value and the argument isn’t the same - My bad

I think you meant hit.Parent though?

If your talking about the main if statement, no, all cells have one of the names listed.

I got Gyazo to work! With print(): https://gyazo.com/0a29482ea27a2ebb16ee5d9cde95c01a

Without: https://gyazo.com/ea828084d8fa86fddb12f7960e22d91b

As you can see, the print() does appear to have an effect. Does anyone have any thoughts?

While testing again, I moved this code to a separate script. This time it worked right consistently! As there is clearly an error with my other code, I have linked it in another thread Issues with Collection Service and my Script (Any Ideas Welcome). (Still no errors though.) Thank you for your time! If you all have any ideas, I will be happy to hear them in my other thread!

Someperson576