I made this script, that if the ball detects that it has touched the ground, it places this “surface” and after it deletes itself. The problem is that if I click from afar, the sphere will go towards that spot while touching the ground multiple times, making multiple duplicated surfaces.
I’ve tried a debounce and it either didn’t work or I’m just bad at making debounces. What I don’t like about having a debounce is that it adds a cooldown (Something which I don’t want).
(I’m new to scripting, so getting tips/feedback from you guys would be amazing!)
Here’s the script:
clone.Touched:Connect(function(hit)
local playable = hit:GetAttribute("playable")
if playable == true then
debris:AddItem(clone,0.1)
local surfaceClone = surfaceRX:Clone()
local rotation = Vector3.new(0, 0, 90)
surfaceClone:SetAttribute("surElement", "water")
surfaceClone.Parent = workspace
surfaceClone.Position = mouseHit.p
surfaceClone.hitBox.Position = mouseHit.p + Vector3.new(0,1.1,0)
surfaceClone.Rotation = rotation
--if surfaceClone.Parent == workspace then
-- local waterEffectClone = waterEffect:Clone()
-- waterEffectClone.Parent = workspace
-- waterEffectClone.Anchored = true
-- wait(0.05)
-- waterEffectClone.Position = surfaceClone.Position
-- makePartBiggerTween:Play()
-- wait(0.2)
-- makePartSmallerTween:Play()
--end
-- Effects/Anims that also don't work because a lot of Touch Detections ^
if hit.Name == "groundSurface" and hit:GetAttribute("surElement") == "fire" then
debris:AddItem(hit,0)
elseif hit.Parent:FindFirstChild("Humanoid") then
local enemyHealth = hit.Parent.Humanoid
enemyHealth:TakeDamage(waterDmg)
end
end
end)
Whenever we detect a hit part with the “playable” attribute set to true is when we create the surface part. Therefore, once we receive confirmation, we don’t want this to fire again. This is a debounce we can implement to prevent multiple triggers:
local hasTriggered = false
clone.Touched:Connect(function(hit)
if hasTriggered then return end --If we've already had a valid detection, we stop the function
local playable = hit:GetAttribute("playable")
if playable == true then --All criteria have been met
hasTriggered = true --So we set the debounce to true
debris:AddItem(clone,0.1)
local surfaceClone = surfaceRX:Clone()
local rotation = Vector3.new(0, 0, 90)
surfaceClone:SetAttribute("surElement", "water")
surfaceClone.Parent = workspace
surfaceClone.Position = mouseHit.p
surfaceClone.hitBox.Position = mouseHit.p + Vector3.new(0,1.1,0)
surfaceClone.Rotation = rotation
if hit.Name == "groundSurface" and hit:GetAttribute("surElement") == "fire" then
debris:AddItem(hit,0)
elseif hit.Parent:FindFirstChild("Humanoid") then
local enemyHealth = hit.Parent.Humanoid
enemyHealth:TakeDamage(waterDmg)
end
end
end)
This also removes the need to depend on the Debris service to handle the part.
Even though this works, the function is meant to constantly be able to fire once again, if I put this it stops the function completely, allowing me to fire the projectile only once.
This helped! Thank you very much, but question… how can I tell where and when to put a debounce? I really don’t understand the concept a lot… or I can’t get it inside my head at least
Whenever all the conditions have been met for an action to take place, that’s the exact instant we set the value of a debounce!
Notice where that moment is in your script: right after we find a hit part with the playable value set to true, we know we can now place the surface part; so if we set the debounce to true, the function will finish completing the action we wanted to happen and we will have prevented the function from executing again.
I practiced a little bit with another function I wanted to do, and I think I got it! Thank you very much! but there’s still this weird thing that the touch + debounce is doing, but I’ll figure it out. Have a good one!
I ran into a problem that I do not know how to fix. (Still with debounce), I thought I got the concept of it, but… I remembered that the script will start over, and whatever the value is set to, it won’t matter if ran again. Therefore, the function will repeat as many times as you start the script.
I’m trying to set a surface, and if it’s already placed and the character is hit once again, it shouldn’t place another surface on top.