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)
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)
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
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
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
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 ;/