Events firing twice in StarterPlayerScripts

Hello scripters.

I am experiencing a problem with my local script, which is that events fire twice, to be exact, when the humanoid changes health.

I tried with .Died and .HealthChanged but same problem.

I even added debounce yet again the same problem.

I first noticed it when the death messages printed twice and confirmed it when I added print() with a letter.

LocalScript:

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:FindFirstChild("Humanoid")

local db = false

local klr = hum:GetAttribute("Killer")

local function death()
	print(klr)
	game.ReplicatedStorage.DeathMessage:FireServer(klr)
end

hum.HealthChanged:Connect(function()
	print("A")
	if db == false then
		db = true
		if hum.Health <= 0 then
			print("b")
			death()
		end
		task.wait(0.1)
		db = false
	end
end)

Thanks if anyone is able to help!

We can have troubles while coding.

I just saw my old self here. Maybe it will take my 20-25 minutes to reply your topic, but it is okay. I’ll be typing those carefully to make you understand all the drama here!

SPOILER

I’ll use advanced english (when I’ll have to be serious of course XD) , you can translate if you don’t know english enough.

Runtime Errors

Issue: Potential Nil Value Access

  • Context: local char = plr.Character or plr.CharacterAdded:Wait()
  • Description: If plr.Character is nil, the code attempts to access the Wait() method on plr.CharacterAdded, which could lead to a runtime error if plr.CharacterAdded is also nil.
  • Suggested Fix: Add a check to ensure plr.Character is not nil before attempting to access plr.CharacterAdded.
    I hope you understood what I mean. Did you…?

Code Optimization Tips

Well, my tips aren’t useful at all but maybe it can be useful for this time.

Issue: Redundant Variable Assignment

  • Context: local db = false
  • Description: The variable db is initialized as false but is only used as a flag to prevent multiple executions of the death function. It could be optimized by directly checking if death has been called.
  • Suggested Fix: Remove the db variable and directly check if death has been executed.

Performance Bottlenecks

What a weird name,right? bOttleNecKs! Joking, let’s go on.

Issue: Redundant Delay in Event Handling

  • Context: task.wait(0.1)
  • Description: Unnecessary delay after the player’s death event.
  • Suggested Fix: Remove the task.wait(0.1) to streamline event handling and improve responsiveness.

Variable Naming

Issue: Inconsistent Variable Naming

  • Context: plr, char, hum, db, klr
  • Description: Variable names follow different naming conventions.
  • Suggested Fix: Maintain a consistent naming style across all variables, like camelCase or snake_case. Ssssss​:snake::snake::snake:!!!

Potential Refactoring Spots

My fingers started to get heavy, and you got tired of reading.

Issue: Conditional Check for Player Death

  • Description: The conditional check for player death and subsequent actions can be refactored for clarity.
  • Suggested Fix: Refactor the logic to handle player death more explicitly and improve readability.

API Misuse

Issue: Incorrect Usage of GetAttribute

  • Context: hum:GetAttribute("Killer")
  • Description: GetAttribute is not a standard method for Humanoid objects.
  • Suggested Fix: Use appropriate methods or properties to retrieve the desired information from the Humanoid object.

Code Smells

I know that “Code Smells” sounds quite awkward.

Issue: Global State Usage

  • Description: The variable db is used as a global flag to control execution flow.
  • Suggested Fix: Consider refactoring to reduce reliance on global state, which can lead to unexpected behavior and make code harder to maintain.

Compatibility Issues

That’s the last advice that I can give you.

Issue: Potential Compatibility with Future Roblox Updates

  • Description: The code may rely on specific Roblox API behaviors that could change in future updates.
  • Suggested Fix: Regularly review and update the code to ensure compatibility with the latest Roblox API changes.

I hope you’re not mad at me for not giving the fixed version of the code. But believe me, copy and paste won’t work. You’ll just forget it easily. If you still cannot fix, let me know.

1 Like

Hello,

thanks for your long reply of suggestions and ways to improve the code but it did not really fix my issue.

I have reduced the code so the function gets called in this way when the player dies: hum.Died:Connect(death) but it did not fix the event calling the function twice.

I removed the “Killer” (klr) variable, or set it to nil for now till I find a fix. I already found a devforum post to get the player who killed the subject but I cannot apply it due to the system used being too complicated.

For the Variable naming tip, I like making my code shorter as it doesn’t fill up everything so much plus I work only (or mostly) with more experienced developers who know who those variable names mean.

So those tips did help a bit but did not fix the issue I am experiencing. Thanks anyways!

Issue:
image
Fires twice at the same time.

2 Likes

Yeah nevermind, I am gonna re-make the script completely.

Edit: Nope, did not fix the remote event firing twice. well, the script detects the humanoid dying twice.

Sorry to hear that, making a new script can work. Maybe…

is there any script that also fires that remoteevent (you can search with CTRL + Shift + F)

Nope, it is a bug, I fixed it by putting a local number and it only sends the message once that number reaches 1, and each time it is received it goes up by 0.5, only a temporary fix until the bug is fixed.