How to make code only print once

I’m trying to print the hit parents name 1 time but it prints it many times

clone.Touched:Connect(function(hit)
      print(hit.Parent.Name)
end)

and I’m new at coding so please explain how I do it simply :sweat_smile:

2 Likes
Part.Touched:Once(function(Hit)
	-- Won't fire again
	print(Hit.Name)
end)
2 Likes

I recommend learning about Debounces as they are key in playing an important role in lots of scripts, although also well-known with the .Touched event.

Debounces allow us to run a function only once, however, you can ‘reset’ a debounce, to have a cooldown effect!

By simply doing:

local db = false
clone.Touched:Connect(function(hit)
if db == false then
   db = true
   print(hit.Parent.Name)
    end
end)

This method will only print once! We check if it has been touched, make the value true, meaning it doesn’t pass the if statement to print it when tounched more then once.

If we were wanting a cooldown (not mentioned here, but I’ll still show you) we can do:

local db = false
clone.Touched:Connect(function(hit)
if db == false then
   db = true
   print(hit.Parent.Name)
   task.wait(5)
   db = false
    end
end)

This method instead, when touched for the first time, will print it, but won’t print if it is stepped again for the next 5 seconds. Once those seconds are up, it can be touched again and print again.

Debounces are a helpful & powerful tool to learn that can be used in many different types of scripts

2 Likes

I want it to print everything it touches once

1 Like

Due to how Roblox’s .Touched works, it will print multiple times as the part moves. You can implement a Debounce as suggested by @OfficialPogCat.

1 Like

You can Simply Do This:

local Hitlist = {}

clone.Touched:Connect(function(hit)
if table.find(Hitlist, hit.Parent) == nil then
table.Insert(Hitlist, hit.Parent)
– Rest Of THe Code Here
end
end)

2 Likes

The script prints all the bodyparts too… I want it to print the name if the character or part

2 Likes

Only by using a debounce variable or using a table of touches:

local debounce = true

clone.Touched:Connect(function(hit)
	if debounce then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			debounce = false
			warn(player)
			task.delay(2, function()
				debounce = true
			end)
		end	
	end
end)
1 Like

In your code, if the part that touches has not a Humanoid still being added in the table.
And the function requires many times to search in the touchedThings table in order to print.
Not really needed to look for a Humanoid if GetPlayer() exist, unless we’re looking for a NPC

Well it kinda works… It only prints the dummies and players name not any parts…

1 Like

Could you provide a video of the script working?

1 Like

I sent you a message!!!

Sorry for the missunderstooding Here is The New Code:

local Hitlist = {}

clone.Touched:Connect(function(hit)
       if table.find(Hitlist, hit.Parent) then return end
      table.insert(Hitlist, hit.Parent)

       -- Rest Of The Code Here
end) ```

This Should Work Perfectlly fine!

Bro, please stop using chatgpt… Honestly, it’s cringe…

1 Like

How were they using ChatGPT lol

I can easily tell!!! :joy::joy::joy::joy::joy:

bro I dont use Chat gpt. I just Got Permission To Post And Reply So if You Dont want People’s Help Then Just Figure It Out Yourself Bud.

1 Like

Have you ever used ChatGPT? No offense to you or who you’re accusing of using ChatGPT, but I doubt ChatGPT would:

  • Misspell “misunderstanding”
  • Randomly capitalize letters in a sentence
  • Misspell “perfectly”
  • Use triple backticks in code formatting
  • Use phrases such as “This should work perfectly fine!” or “Rest of the code here”
  • Use names such as “clone” that match the original code sample
  • Add extra spaces to indents
  • Write one line guard clauses (hard to read for some people)

You get the idea.
Here’s an actual script generated by ChatGPT:

local part = script.Parent -- Replace 'script.Parent' with the path to the part you want to monitor

local touched = false -- Variable to keep track of whether the part has been touched or not

local function onTouch(otherPart)
    if otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
        if not touched then -- Check if the part has been touched before
            touched = true -- Set touched to true to prevent multiple prints

            print("Hello!")
        end
    end
end

part.Touched:Connect(onTouch)

Prompt: Can you generate a Roblox script that prints "Hello!" one time when a part gets touched?

If you can do the code your self and do not need help from me then do it ur self bud :rofl:

1 Like
while true do
	task.wait(0.1) -- You may want to change this to be more performant
	local result = script.Parent:GetTouchingParts() -- requires part to be collidable
	if #result < 1 then continue end

	-- do stuff

	break
end

You’ll want to use WorldRoot:GetPartsInPart if you’re doing some kind of big detection (like a kill zone), but this should work splendid if it’s something like a kill brick.