Why does my remote event fire 3 times from humanoid.died?

whats happening is that i am trying to make a mob system and when its killed it fires a event but it does this multiple times, it stacks and makes the game unplayable in fps

firing code

local hum = script.Parent.Enemy
local mob = script.Parent.Name
local done = false

hum.Died:Connect(function()
	if not done then
	local newman = game.ReplicatedStorage.EnemiesStorage:FindFirstChild(""..mob):Clone()
		done = true
		game.ReplicatedStorage.Remotes.MobKilled:FireClient(game.Players:GetPlayerByUserId(script.Parent.HitterID.Value))
	local worth = script.Parent.WorthAmount.Value
	local location = math.random(1,3)
	wait(2)
	if location == 1 then
		newman.Parent = script.Parent.Parent
		newman.HumanoidRootPart.CFrame = script.Parent.Parent.ReturnPoint1.CFrame
		wait(0.1)
		script.Parent:Destroy()
	elseif location == 2 then
		newman.Parent = script.Parent.Parent
		newman.HumanoidRootPart.CFrame = script.Parent.Parent.ReturnPoint2.CFrame
		wait(0.1)
		script.Parent:Destroy()
	elseif location == 3 then
		newman.Parent = script.Parent.Parent
		newman.HumanoidRootPart.CFrame = script.Parent.Parent.ReturnPoint3.CFrame
		wait(0.1)
		script.Parent:Destroy()
	end
		end	

end)

receiving code

local db = false
local tweeninfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Back,
	Enum.EasingDirection.Out,
	0,
	false,
	0

)

game.ReplicatedStorage.Remotes.MobKilled.OnClientEvent:Connect(function(a, worth)
	if not db then
		db = true
	local Tag = game.Players.LocalPlayer.PlayerGui.MainGui.Buttons.AmountCollectedMob:Clone()
	local goal = {}
		goal.Position = UDim2.new(0.43, 0, -0.180, 0)
		print("defeated")
	local tween = game:GetService("TweenService"):Create(Tag, tweeninfo, goal)
	Tag.Parent = game.Players.LocalPlayer.PlayerGui.MainGui.Buttons
	Tag.Text = "+ "..math.random(1, 4000)
	tween:Play()
	game.Players.LocalPlayer.leaderstats.Cash.Value += 200
		wait(2)
		db = false
		Tag:Destroy()
		end
end)

any help is appreciated

1 Like

Are you sure that the event is firing three times? Or is it that the code within your eventHandler is firing multiple times?

i think that its the event, because even when i debounce the eventhandler it still does that

Without reading your code, I can say with relative certainty that it’s just a stupid bug relating to humanoid.Died's implementation internally.

I had a similar problem years ago when scripting a leaderboard which needed to count deaths and I was plagued with “triple counting” deaths. The fix is pretty simple:

local dead = false;
hum.Died:Connect(function()
    if dead then return end;
    dead = true;
    -- do stuff
end)

We’re basically just using a debounce so it can only run once. It’s stupid that you have to deal with this, but it’s just how it is.

after i did that the same problem is still coming
image
ima try to see if it is the problem in the eventhandler now
edit: idk whats going on i dont think its the eventhandler

With that out of the way (and by the way, I always add a debounce to the died event, because I don’t trust it after the triple counting bug my community and I faced years ago), check to see if some code is running more than once within the client’s code for handling the event.

Be sure to post your EventHandler code. We can’t help if we can’t see it. You can edit your post above to include it.

i just added it, thank you for reminding

1 Like

Where is your eventHandler stored? Is there more than one of these scripts? For instance, does each mob have its own eventHandler? If so, you need a way of differentiating between which mobs fire the event. Otherwise, each eventHandler you have is receiving the same event. If the event looks like it’s being fired three times, it could be because there are three different eventHandlers receiving the same single event.

Add a debounce that it only fires if its true and set it to false once you run it.

the thing that recieves the event is the mob, it goes to a textlabel to make a popup, can you make an example of what you mean cause i dont really get it

Basically, I’m asking if you have multiple mobs. If not, then what I’m talking about isn’t the problem.

i do have multiple mobs
the third ty char

If you have multiple mobs, and each one has its own receiving script, then you are essentially doing the following multiple times, which is resulting in print("defeated") being printed multiple times:

game.ReplicatedStorage.Remotes.MobKilled.OnClientEvent:Connect(function(a, worth) ...
	

how can i stop this loop from happening
each mob fires it, but there is only one script recieving everything

This part is your problem, I think. You are cloning the mob that has 0 health, resulting in multiple hum.Died events being called.

oo, maybe not. I see you’re cloning from ReplicatedStorage now. My bad.

I think the issue here is the way that you are spawning new mobs. It looks like you are cloning the mob that is already in the game, which will result in the script referencing the original mob rather than the clone.

i do have multiple mobs

If the event fires same amount of times as the amount of mobs in the game, this is very likely Your problem.

To solve this, you would need to make sure Your scripts DO NOT run before you actually parent new cloned mob in the workspace. That is, either clone mobs from the ServerStorage or do not parent your script to the models. Also make sure Your scripts are in the “Legacy” RunContext (default) if you decide to go with the first solution.

the mob is actually a clone from replicatedstorage

1 Like

im still on this, is there even any way to fix this or is it a logic error