part.Touched triggering

i want the following script to deal damage only when the value is true, then destroy the part that’s been touched, but when i print how many times the part has been touched by the right player (in line 3) by printing something after i set the value to false, it prints atleast 100 times.

part.Touched:Connect(function(hit)
	if hit and hit.Parent then
		if hit.Parent.Name == character.Name then
			if value == true then
				value = false
				print("a")
				wait()
				humanoid:TakeDamage(20)
				wait()
				part:Destroy()
				value = true
			end
		end
	end
end)

The Output :

a (x232)

You should also fix it like so, (i removed the debounce because I believe you don’t need it for this situation)

local part = -- reference the part here
local name = -- reference a character and put .name

local value = false

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- checks if its a character
        if hit.Parent.Name == name then -- checks if the character's name matches up with the name earlier
            if value == true then
		     print("a") -- for bug testing
		     hit.Parent.Humanoid:TakeDamage(20) -- deals damage
		     part:Destroy() -- destroys part
          end
       end
	end
end)

Proof of it working: video of what should happen

2 Likes

Firstly, I’d refrain from using wait() and instead using task.wait() or RS.Heartbeat:Wait() (RS being game:GetService(“RunService | Roblox Creator Documentation”)

Secondly, you’re making your variable “value” true which will allow the function to continue to run as long as the player is touching the part. To solve this, you can add wait(3) or something before changing the value to true again.

However, as you’re destroying the part in this function the function shouldn’t be running more than once as the part is being destroyed. I’m not quite sure why it isn’t being destroyed or why the function is still running but I’ve gone ahead and tested it in studio how I would do it and it seems to work fine.
The script is inside a part in workspace

value = true
script.Parent.Touched:Connect(function(hit)
	if hit.Parent == nil then 
		print("nil")
		return 
	end
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if (humanoid ~= nil) then -- Checks if the part has a humanoid (basically checking if only a player touches the part)
		if value == true then
			value = false -- makes your variable false
			print("a")
			humanoid.Health -= 20 -- Damages the player 20 health
			script.Parent:Destroy() -- Deletes the part
			value = true -- makes your variable true
		end
	end
end)

If you’re making multiple parts, then you can add the above into a loop so the function would run if any part is touched, rather than having to have multiple scripts in each part which would look something like

for i,part in ipairs(workspace.DamageParts:GetChildren()) do -- damage parts inside a folder named "DamageParts" in workspace
    -- script above in here but replace "script.Parent.Touched" to "part.Touched"
end

You misspelled FindFirstChild.

1 Like

with or without the debounce, the player still takes damage, even if the value is set to false. And the issue is still the same

Check my code, I even made a video showcasing it.

it didnt work before you edited it, let me try it out!

Also, remember that you have to change the local value = true so that it works.

yeah i just noticed it, thanks!

Just do “if value then” not “if value == true”

That isn’t what he’s looking for.

Try using ZonePlus by the HD Admin dude.

You don’t need ZonePlus for this simple of a script.

He should follow the devforum rules more clearly so people don’t get confused about what he needs

you simply didnt read the description well

1 Like

the damage being dealt more than once had to do with my script (which isnt inside the part) if it worked for you. so i moved it to the part, and it still does the same thing

also in these lines

local value = false
	if hit.Parent:FindFirstChild("Humanoid") then -- checks if its a character
		if hit.Parent.Name == name then -- checks if the character's name matches up with the name earlier
			if value == true then

I never set the value to true, yet it still activated?

Sorry you have no respect (something you can learn). I have supported you and your script as this is scripting support. I fixed an issue I saw and you didn’t therefore you should show even more gratitude. I hope for your sake that you did not mean impertinence in that message

Can you send me the whole code that you copied? because that is not supposed to happen.

i meant no offence with what i said, i thought you were replying to this part.Touched triggering - #9 by Katrist
I double checked and you were not, Sorry ;/