Humanoid:Died() dosen't work right

Hello I am making a custom gun script and right now I am working on ViewModel, everything works right until i wanted to Kill my character, according to my script it should Destroy the ViewModel but it dosen’t even call “Died()” event but second time I die everything works right, any ideas where is the problem?

local localplr = game.Players.LocalPlayer
local UserInput = game:GetService("UserInputService")
local character = localplr.Character or localplr.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local Events = game.ReplicatedStorage.NewEvents
local shootEvent = Events.Shoot
local EquipViewModelEvent = Events.EquipViewModel



UserInput.InputBegan:Connect(function(input, gameProcessEvent)
    if not gameProcessEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
        local mouse = localplr:GetMouse()
        local rayOrigin = head.Position
        local rayDirection = (mouse.Hit.Position - rayOrigin).unit * 80000
        local raycastParams = RaycastParams.new()
        raycastParams.FilterDescendantsInstances = {character}
        raycastParams.IgnoreWater = true
        local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
        if raycastResult then
            local partHit = raycastResult.Instance
            local distance = (rayOrigin-raycastResult.Position).magnitude
            local position = raycastResult.Position
            shootEvent:FireServer(partHit, position, distance)
        end
    end
end)

EquipViewModelEvent.OnClientEvent:Connect(function(ViewModel: Model)
    local Camera = workspace.CurrentCamera    
    local loop

    localplr.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")

        humanoid.Died:Connect(function()
            print("dead xD")
            if loop then
                loop:Disconnect()
                ViewModel:Destroy()
            end
        end)
    end)

    loop = game:GetService("RunService").RenderStepped:Connect(function()
        if localplr.Character and localplr.Character:FindFirstChild("Humanoid") then
            ViewModel:PivotTo(Camera.CFrame)
        end
    end)
end)
1 Like

This should be in #help-and-feedback:scripting-support

1 Like

You need to connect the .Died event to the current humanoid outside of the characteradded function

1 Like

oop, sorry, fixed, i barely use dev forum so thats why

1 Like

i kind of don’t understand, I tried this but it dosen’t seem to work so I guess you mean’t something diffrent

EquipViewModelEvent.OnClientEvent:Connect(function(ViewModel: Model)
	local Camera = workspace.CurrentCamera	
	local loop
	local humanoid = localplr.character:WaitForChild("Humanoid")
	localplr.CharacterAdded:Connect(function(char)

		humanoid.Died:Connect(function()
			print("dead xD")
			if loop then
				loop:Disconnect()
				ViewModel:Destroy()
			end
		end)
end)
1 Like

remove
localplr.CharacterAdded:Connect(function(char)

2 Likes

Isn’t Humanoid:Died an one time event ? I don’t remember this event correctly so tell me if i am incorrect

1 Like

if the humanoid dies then your character gets removed and therefore a new humanoid gets made

1 Like

So let me do something rq , correct me please, this is just an example for myself to understand this event better
game:service’Players’.PlayerAdded:Connect(function(v)
local chr=v.Character or v.CharacterAdded:Wait()
chr:WaitForChild’Humanoid’:Died:Connect(function()
local plr=game:service’Players’:GetPlayerFromCharacter(chr)
plr.CharacterAdded:Connect(function()
plr.Character:FindFirstChild’Humanoid’:Died:Connect(function() print’died’ end)
end)
end)
end)-- personally think this won’t work

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.