Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this event only happen every other time?

The Humanoid.Died Event has been a nuisance to me when I'm making my death screen script. The script itself works perfectly fine however I've narrowed the problem down to see why my script only worked every other time i reset my character.

PLAYER_MODEL.Died:connect(function()
	print(Death)
	if not Death then
		Death = true
		DiedEvent:FireClient(player, Death)
	end
end)

On line 2 Death would only print false every other time my character reset.

My question is, is there any way to fix this?

To give you a visual

OUTPUT:

--character reset

--nil

--character reset

--character reset

--false

--character reset

--character reset

--false

2 comments

0

I think whats happening here is the script reseting itself if its in StarterCharacterScripts, i don't really get what you mean tho

0

the script is in ServerScriptService, and everytime the character presumably kills himself the death screen only works every other time @Igoralexeymarengobr

1 answer

✓ Accepted answer

GENERAL ISSUES

Use :GetService() to retrieve the Players Service

ISSUES

connect is deprecated in favor of Connect

REVISED SERVER SCRIPT

local DiedEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local Death = false

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			print(Death)
			if Death == false then
				Death = true
				DiedEvent:FireClient(player, Death)
			end
		end)
	end)
end)

Keep in mind that I had to add a local variable for Death and DiedEvent without knowing where they were / their names, as well as a PlayerAdded / CharacterAdded Event.

I will also note that this function will only run once based on how it is written. If you want it to occur every time the player dies, simply remove the if-then statement.

local DiedEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local Death = false

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			Death = true
			print(Death)
			DiedEvent:FireClient(player, Death)
			Death = false
		end)
	end)
end)

If there are any other issues, you will have to release more of the code you are using since there are relatively few problems with what has been provided.

1 comment

0

ironically i already fixed this issue, but i'll still give you credit for a good answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.