In this script, a part gets spawned into the cframe of another (already existing part) with the same size, then gets deleted. It works on debounce and an “On and off” switch. The script works other then the fact that its “spammable” and can be used many times. When this happens, it makes another part onto the same area, instead of deleting the last one. I have tried adding an extra check, but It didnt work, I am very confused on why this script doesnt work.
The script:
local Detection = script.Parent.ClickDetector
local LocationWater = script.Parent.Parent.Location
local OnOff = "off"
local DebounceOn = false
Spawnedwater = ""
Detection.MouseClick:Connect(function()
Spawnedwater = Instance.new("Part")
if OnOff == "off" then
if DebounceOn == false then
DebounceOn = true
-- Specifications for water (so extra)
Spawnedwater.CFrame = LocationWater.CFrame
Spawnedwater.Size = LocationWater.Size
Spawnedwater.CanCollide = false
Spawnedwater.Anchored = true
Spawnedwater.BrickColor = BrickColor.new("Baby blue")
Spawnedwater.Material = Enum.Material.Sand
Spawnedwater.Parent = script.Parent.Parent
wait(0.5)
DebounceOn = false
OnOff = "on"
end
end
end)
Detection.MouseClick:Connect(function()
if OnOff == "on" then
if DebounceOn == false then
DebounceOn = true
OnOff = "off"
Spawnedwater:Destroy()
wait(0.5)
DebounceOn = false
end
end
end)
if Debounce then Debounce = not Debounce -- if debounce == true then make it false and move on.
if OnOff == "off" then warn"MACHINE IS POWERLESS" return end -- end here if OnOff == "off".
...--Code Here
Delay(0.5,function()
Debounce = not Debounce
end)
else warn"DEBOUNCE REJECTED" return Debounce -- alt warning for clicks during debounce
end
Try adapting something like this into it maybe? I do think it could be wrapped into a single function. Unless I am mistaken.
Maybe just move
Spawnedwater = Instance.new(“Part”)
into the debounce. Not sure if it will help, but it would be worth a try. Also make the two functions into the same function, making them into two is not needed.
local debounce : boolean
local function main()
debounce = not debounce
if debounce then return end
if script.Parent.Parent:FindFirstChild("water") then
-- destroy
else
-- spawn water
end
task.wait(0.5)
end
script.Parent.ClickDetector.MouseClick:Connect(main)
Try this. I merged the two :Connect functions, and changed around some checks so you’re now checking if DebounceOn isn’t activated first, then doing stuff depending on what OnOff is. After you do what you need based off of OnOff, we wait for half a second and turn OnOff to “off” and DebounceOn to false.
Edit: changed Spawnedwater to be nil at the start, then be assigned at the beginning to either what it already is, or a new part. I also set Spawnedwater to be nil after we destroy it, just in case.
(edit 2 & 3: formatting)
local Detection = script.Parent.ClickDetector
local LocationWater = script.Parent.Parent.Location
local OnOff = "off"
local DebounceOn = false
Spawnedwater = nil
Detection.MouseClick:Connect(function()
Spawnedwater = Spawnedwater or Instance.new("Part")
if DebounceOn == false then
DebounceOn = true
if OnOff == "off" then
-- Specifications for water (so extra)
Spawnedwater.CFrame = LocationWater.CFrame
Spawnedwater.Size = LocationWater.Size
Spawnedwater.CanCollide = false
Spawnedwater.Anchored = true
Spawnedwater.BrickColor = BrickColor.new("Baby blue")
Spawnedwater.Material = Enum.Material.Sand
Spawnedwater.Parent = script.Parent.Parent
else
OnOff = "off"
Spawnedwater:Destroy()
Spawnedwater = nil
end
wait(0.5)
DebounceOn = false
OnOff = "on"
end
end)