Connection isn't deconnecting (Maids)

I have a function like this:

local maidClass = Maid.new()
local Debounce = false
local function GolemnHit(hitPart)
	if Debounce or not hitPart.Parent:FindFirstChild("Humanoid") then return end
	debounce = true

	hitPart.Parent.Humanoid:TakeDamage(5)
    wait(5)
	Humanoid.WalkSpeed = 16

	maidClass:DoCleaning() 
    debounce = false
	return
end

for _,Part in ipairs(Golem:GetDescendants()) do 
	if not Part:IsA("BasePart") then return end
	maidClass:GiveTask(Part.Touched:Connect(GolemnHit))
end

I am making a boss fight where the boss charges on the player and damage it on touch. My boss should only damage it once, but here it‘s damaging the player over 10 times, wich is too much (I set 5 to see how much is the boss damaging, and it spam attacks, leaves the player with only 50 HP, then dosen‘t waits 5 seconds but starts again to attack, wich kills the player in 3 seconds).

I added a debounce to the function, but it dosen‘t seem to works, because even if the debounce is true and that it should stop, it just ignores it. What‘s the problem? I need to deconnect the event to make it stops damaging the player, so I am using Maids, but it isn‘t deconnecting, wich confuses me.

4 Likes

Roblox has a disconnect function for connections. Here is a link to it.

https://developer.roblox.com/en-us/recipes/How-to-disconnect-an-event-connection

2 Likes

I tried it, but apparently Maids are better. And they use the Disconnect function:

But thanks for trying! My problem is that in the line with maidClass:DoCleaning() the Event should disconnect (If I understod maids enough), but it isn‘t working. If I would use the Disconnect function, I would have a code over 40 lines, wich I had but was a true mess.

I have used maids before and I’m pretty sure they disconnect, I believe the problem here is the order at which the lines are running within the function relative to the yield.

local Debounce = false
local function GolemnHit(hitPart)
	if Debounce or not hitPart.Parent:FindFirstChild("Humanoid") then return end
	Debounce = true --missed a capitalization here

	hitPart.Parent.Humanoid:TakeDamage(5)
	Humanoid.WalkSpeed = 16
	maidClass:DoCleaning() 

    wait(5)
    Debounce = false
	return
end

The disconnect should be happening immediately if you want to prevent the previous connections from occurring. Also I believe there is a mispelling with the debounce as it should be capitalized according to the debounce variable so yeah it may trigger multiple times in a row.

3 Likes

I also had something like this happen to me when I was doing something similiar to this.

1 Like

Ok, leave me try it out. But I normally write debounce in lowerletters, I think I just wrote it manually wrong.

1 Like

@dthecoolest, I checked my Code and I wrote the same only everything in lowletters, so I tested the game and no, the bug is still running. Nothing changed, so I don‘t think it disconnected.

I think your problem is in this line:

if Debounce or not hitPart.Parent:FindFirstChild(“Humanoid”) then return end

probably because if there was a humanoid then it would continue running.

1 Like

Ok, I started printing this whole line out, to see if it was true or false, and it was false. Now I finally have a. Way to check. It turns out that both of them are false. I would understand that not hitPart.Parent:FindFirstChild("Humanoid") is false, because the player is still alive, but the debounce should be true. And if I print it out, it isn‘t! It changes to true, but when the next touched event is fired, it tourns to false. So, the debounce isn‘t updating, but why? Is it because this function is local?

Edit: No, even change the function to a global function, the result is the same: The debounce variable changes to true only while the event is running, after this it gets false. And when the debounce is false then the loop dosen‘t stop.

I think that the problem is because your giving each individual part inside of the golem the touch connection so the player gets damaged multiple times.

1 Like

It‘s normal, because I can‘t predict what part accurately touches the player, so while the boss is charging, every part does damage. But after the damage is done the event needs to be disconnected to avoid that the other parts do damage too.

But the maid isn‘t deconnecting, this means that the event still runns, wich means that it still damages the player.

1 Like

So, ok. Does your golem have a hitbox or something? Because I think it would be the most accurate

What do you mean? I want to keep it accurate, so I made that every part of the golem becomes deadly. I am testing this on a normal R15 dummy before trying this to a custom character and no, I didn‘t add something extra to the dummy.

Edit: I tried to make that after the damage it disconnect all connections, and it dosen‘t work.

Oh, so thats how you want it. I dont really know how to disconnect all the connections on every part that has one once a certain thing happens. I would usually go with making an individual part that scales as big as the model with a touch event.

1 Like

I tried this before and I had the same problem + It wasn‘t accurate, so I removed it.

Oh, then I think you best option is ask other people for help or look at the developer Hub for more information on this. I think others can find a better answer to your question than me. :wink:

2 Likes

This is the reason of this post…

But, I really don‘t understand why a connection isn‘t disconnecting, I worked some weeks ago on another thing where I needed to disconnect an event, it showed me that the event was disconnected, but it didn‘t helped me out. I tried yesterday with math, wich “worked”, but even if you dodge it it did you damage because It was like a hitbox (and the code was too long)

What I am trying to say is that why event disconnecting dosen‘t work when I try them?

1 Like

I don’t think I know a way to disconnect all existing connections on the models parts using maid class. Also sorry for not telling you in advanced that I don’t really know how to use Maids.

1 Like

Probably the problem is “wait(5)” which yields the thread that launched the event, allowing it to run more than once. Debounce is also not working because of a spelling error. If you disconnect the event immediately, you don’t need to debounce and the event thread will only run once.

local function GolemnHit(hitPart)
	if not hitPart.Parent:FindFirstChild("Humanoid") then return end
	maidClass:DoCleaning()

	hitPart.Parent.Humanoid:TakeDamage(5)
	maidClass:DoCleaning()
	wait(5)
	Humanoid.WalkSpeed = 16

	return
end

Keep in mind that you are disconnecting all touch events at the same time and not just the event that was fired.

2 Likes

After trying to solve it, I am seeing that the problem is the debounce variable. Like I suspected, after the Event is done it turns from true to false, wich allows the loop to be played again. I can‘t understand why. And no, I tried this and it dosen‘t solve the problem. I will try to continue to dig, maybe I should first remove maids.

This is why debounce needs to stop the damage while it‘s disconnecting everything.