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
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
Part.Touched:Once(function(Hit)
-- Won't fire again
print(Hit.Name)
end)
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
I want it to print everything it touches once
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.
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)
The script prints all the bodyparts too… I want it to print the name if the character or part
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)
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…
Could you provide a video of the script working?
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…
How were they using ChatGPT lol
I can easily tell!!!
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.
Have you ever used ChatGPT? No offense to you or who you’re accusing of using ChatGPT, but I doubt ChatGPT would:
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
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.