Problem with restoring character states

Hello, i have a tool which when it lands on a character, it will perform a mauling action. But the problem is whenever the victim(player) or the tool player leaves the game or changing team, respawned, removed both of them wouldnt get restored and they will continue to do the mauling action.

Here is the main part about it inside the code:

jumpremote.OnServerEvent:Connect(function(plr, hitsmth)
	local toolowner = getToolOwnerPlayer(tool)
	local char = toolowner.Character or toolowner.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local root = char:FindFirstChild("HumanoidRootPart")

	local victim = hitsmth.Parent
	local humofvictim = victim:FindFirstChildOfClass("Humanoid")
	local rootofvictim = victim:FindFirstChild("HumanoidRootPart")

	local ownerstats = char:FindFirstChild("Stats")

	local attmultiplier = (ownerstats and ownerstats:FindFirstChild("AttackMultiplier")) and ownerstats.AttackMultiplier.Value

	local victimstats = victim:FindFirstChild("Stats")
	local victimdefend = (victimstats and victimstats:FindFirstChild("Defensive")) and victimstats.Defensive.Value 

	if not humofvictim or not rootofvictim then return end

	local cmaultrack = humanoid:LoadAnimation(cmaulanim)
	local vmaultrack = humofvictim:LoadAnimation(vmaulanim)

	local victimPlayer = players:GetPlayerFromCharacter(victim)
	if victimPlayer and isSameTeam(toolowner, victimPlayer) then return end

	char:SetAttribute("tanked")

	cmaultrack:Play()
	vmaultrack:Play()
	
	connection:Disconnect()
	root.Anchored = true
	rootofvictim.Anchored = true
	
	local pointlight = Instance.new("PointLight")
	pointlight.Color = Color3.new(1, 0, 0)
	pointlight.Parent = rootofvictim
	pointlight.Brightness = 10
	pointlight.Range = 15

	if humofvictim and humofvictim.Health > 0 and humanoid.Health > 0 then
		while humofvictim.Health > 0 and humanoid.Health > 0 do
			local damage = damagecal:Calculate(mauldmgper,attmultiplier,victimdefend)
			humofvictim:TakeDamage(damage)
			maul:Play()

			humanoid.AutoRotate = false
			humofvictim.AutoRotate = false
			victim:SetAttribute("CanAttack", false)
			char:SetAttribute("CanAttack", false)

			if root.Anchored ~= true or rootofvictim.Anchored ~= true then
				rootofvictim.Anchored = true
				root.Anchored = true
			end

			dealdmgremote:FireClient(toolowner, victim, damage)

			root.CFrame = CFrame.new(root.Position, Vector3.new(rootofvictim.Position.X, root.Position.Y, rootofvictim.Position.Z))
			rootofvictim.CFrame = root.CFrame * CFrame.new(0, 0, -1)
			rootofvictim.CFrame = CFrame.new(rootofvictim.Position, Vector3.new(root.Position.X, rootofvictim.Position.Y, root.Position.Z))
			task.wait(0.3)
		end

		if humofvictim.Health <= 0 then
			char:SetAttribute("CanAttack", true)
			char:SetAttribute("tanked", nil)
			victim:SetAttribute("CanAttack", true)
			humanoid.AutoRotate = true
			humofvictim.AutoRotate = true
			rootofvictim.Anchored = false
			root.Anchored = false
			cmaultrack:Stop()
			vmaultrack:Stop()
			pointlight:Destroy()
		end

		if humanoid.Health <= 0 then
			char:SetAttribute("CanAttack", true)
			char:SetAttribute("tanked", nil)
			victim:SetAttribute("CanAttack", true)
			humanoid.AutoRotate = true
			humofvictim.AutoRotate = true
			rootofvictim.Anchored = false
			root.Anchored = false
			cmaultrack:Stop()
			vmaultrack:Stop()	
			pointlight:Destroy()
		end
	end
end)
1 Like

can somebody reply and give me the way to solve this :smiling_face_with_tear:

if your remote event is firing once these won’t go off since you already have more than 0 in health

from this if humofvictim and humofvictim.Health > 0 and humanoid.Health > 0 then

you need to use the Humanoid.Died event or use Humanoid.Changed event for those Humanoids then put the code i quoted (above) inside that event so it stops the animation.

will it fixes the changing team and respawn event?

try and see. Idk since i dont have your rbx place. also its hard to tell how your firing your event you could just use a elseif statement in

jumpremote.OnServerEvent:Connect(function(plr, hitsmth)
	local toolowner = getToolOwnerPlayer(tool)
	local char = toolowner.Character or toolowner.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local root = char:FindFirstChild("HumanoidRootPart")

	local victim = hitsmth.Parent
	local humofvictim = victim:FindFirstChildOfClass("Humanoid")
	local rootofvictim = victim:FindFirstChild("HumanoidRootPart")

	local ownerstats = char:FindFirstChild("Stats")

	local attmultiplier = (ownerstats and ownerstats:FindFirstChild("AttackMultiplier")) and ownerstats.AttackMultiplier.Value

	local victimstats = victim:FindFirstChild("Stats")
	local victimdefend = (victimstats and victimstats:FindFirstChild("Defensive")) and victimstats.Defensive.Value 

	if not humofvictim or not rootofvictim then return end

	local cmaultrack = humanoid:LoadAnimation(cmaulanim)
	local vmaultrack = humofvictim:LoadAnimation(vmaulanim)

	local victimPlayer = players:GetPlayerFromCharacter(victim)
	if victimPlayer and isSameTeam(toolowner, victimPlayer) then return end

	char:SetAttribute("tanked")

	cmaultrack:Play()
	vmaultrack:Play()

	connection:Disconnect()
	root.Anchored = true
	rootofvictim.Anchored = true

	local pointlight = Instance.new("PointLight")
	pointlight.Color = Color3.new(1, 0, 0)
	pointlight.Parent = rootofvictim
	pointlight.Brightness = 10
	pointlight.Range = 15

	if humofvictim and humofvictim.Health > 0 and humanoid.Health > 0 then
		while humofvictim.Health > 0 and humanoid.Health > 0 do
			local damage = damagecal:Calculate(mauldmgper,attmultiplier,victimdefend)
			humofvictim:TakeDamage(damage)
			maul:Play()

			humanoid.AutoRotate = false
			humofvictim.AutoRotate = false
			victim:SetAttribute("CanAttack", false)
			char:SetAttribute("CanAttack", false)

			if root.Anchored ~= true or rootofvictim.Anchored ~= true then
				rootofvictim.Anchored = true
				root.Anchored = true
			end

			dealdmgremote:FireClient(toolowner, victim, damage)

			root.CFrame = CFrame.new(root.Position, Vector3.new(rootofvictim.Position.X, root.Position.Y, rootofvictim.Position.Z))
			rootofvictim.CFrame = root.CFrame * CFrame.new(0, 0, -1)
			rootofvictim.CFrame = CFrame.new(rootofvictim.Position, Vector3.new(root.Position.X, rootofvictim.Position.Y, root.Position.Z))
			task.wait(0.3)
		end


	elseif humofvictim.Health <= 0 or humanoid.Health <= 0 then
		
	

		if humofvictim.Health <= 0  then
			char:SetAttribute("CanAttack", true)
			char:SetAttribute("tanked", nil)
			victim:SetAttribute("CanAttack", true)
			humanoid.AutoRotate = true
			humofvictim.AutoRotate = true
			rootofvictim.Anchored = false
			root.Anchored = false
			cmaultrack:Stop()
			vmaultrack:Stop()
			pointlight:Destroy()
		end

		if humanoid.Health <= 0 then
			char:SetAttribute("CanAttack", true)
			char:SetAttribute("tanked", nil)
			victim:SetAttribute("CanAttack", true)
			humanoid.AutoRotate = true
			humofvictim.AutoRotate = true
			rootofvictim.Anchored = false
			root.Anchored = false
			cmaultrack:Stop()
			vmaultrack:Stop()	
			pointlight:Destroy()
		end
	end
end)
1 Like

what does “Changed” do with Humanoid

gets the changes in the property of humanoid, so if your health is changed from 10 - 5 it can allow to do this.

Humanoid.Changed:Connect(function())

if Humanoid.Health =< 0 then

print("Humanoid Health:"..Humanoid.Health")

end
end

but i need to detect if the tool owner player or the victim player has respawned or something close to that

explain more, like a print statement or player.CharacterAdded event

edit: fixed if statement placement

when i respawn while the mauling action is still performing the victim would get stuck and the maul animations, root part anchoring and autorotate wouldnt get restored like when they died, the dead handler works fine i dont think you need to change it

i can send a video if needed

i think i know what u mean, pls send a video anyway!

i think its because of your while loop -

jumpremote.OnServerEvent:Connect(function(plr, hitsmth)
	local toolowner = getToolOwnerPlayer(tool)
	local char = toolowner.Character or toolowner.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local root = char:FindFirstChild("HumanoidRootPart")

	local victim = hitsmth.Parent
	local humofvictim = victim:FindFirstChildOfClass("Humanoid")
	local rootofvictim = victim:FindFirstChild("HumanoidRootPart")

	local ownerstats = char:FindFirstChild("Stats")

	local attmultiplier = (ownerstats and ownerstats:FindFirstChild("AttackMultiplier")) and ownerstats.AttackMultiplier.Value

	local victimstats = victim:FindFirstChild("Stats")
	local victimdefend = (victimstats and victimstats:FindFirstChild("Defensive")) and victimstats.Defensive.Value 

	if not humofvictim or not rootofvictim then return end

	local cmaultrack = humanoid:LoadAnimation(cmaulanim)
	local vmaultrack = humofvictim:LoadAnimation(vmaulanim)

	local victimPlayer = players:GetPlayerFromCharacter(victim)
	if victimPlayer and isSameTeam(toolowner, victimPlayer) then return end

	char:SetAttribute("tanked")

	cmaultrack:Play()
	vmaultrack:Play()

	connection:Disconnect()
	root.Anchored = true
	rootofvictim.Anchored = true

	local pointlight = Instance.new("PointLight")
	pointlight.Color = Color3.new(1, 0, 0)
	pointlight.Parent = rootofvictim
	pointlight.Brightness = 10
	pointlight.Range = 15

	if humofvictim and humofvictim.Health > 0 and humanoid.Health > 0 then
			while humofvictim.Health > 0 and humanoid.Health > 0 do
				local damage = damagecal:Calculate(mauldmgper,attmultiplier,victimdefend)
				humofvictim:TakeDamage(damage)
				maul:Play()

				humanoid.AutoRotate = false
				humofvictim.AutoRotate = false
				victim:SetAttribute("CanAttack", false)
				char:SetAttribute("CanAttack", false)

				if root.Anchored ~= true or rootofvictim.Anchored ~= true then
					rootofvictim.Anchored = true
					root.Anchored = true
				end

				dealdmgremote:FireClient(toolowner, victim, damage)

				root.CFrame = CFrame.new(root.Position, Vector3.new(rootofvictim.Position.X, root.Position.Y, rootofvictim.Position.Z))
				rootofvictim.CFrame = root.CFrame * CFrame.new(0, 0, -1)
				rootofvictim.CFrame = CFrame.new(rootofvictim.Position, Vector3.new(root.Position.X, rootofvictim.Position.Y, root.Position.Z))
				task.wait(0.3)

				if humofvictim.Health <= 0 or humanoid.Health <= 0 then
					
				if humofvictim.Health <= 0  then
					char:SetAttribute("CanAttack", true)
					char:SetAttribute("tanked", nil)
					victim:SetAttribute("CanAttack", true)
					humanoid.AutoRotate = true
					humofvictim.AutoRotate = true
					rootofvictim.Anchored = false
					root.Anchored = false
					cmaultrack:Stop()
					vmaultrack:Stop()
					pointlight:Destroy()
				end

				if humanoid.Health <= 0 then
					char:SetAttribute("CanAttack", true)
					char:SetAttribute("tanked", nil)
					victim:SetAttribute("CanAttack", true)
					humanoid.AutoRotate = true
					humofvictim.AutoRotate = true
					rootofvictim.Anchored = false
					root.Anchored = false
					cmaultrack:Stop()
					vmaultrack:Stop()	
					pointlight:Destroy()
				end
					
					break --//Stops the while loop
				end
			end

	end
end)
1 Like
External Media

I guess this video is simple to understand what i meant

yeah it should be the issue i sent replace that could with your while loop you have, imma edit it - hold up.

i have made an edit on this idk if it works - test tho, thats the whole code

well the dead handler work, but the respawning problem remain the same :frowning_face:

jumpremote.OnServerEvent:Connect(function(plr, hitsmth)
	local toolowner = getToolOwnerPlayer(tool)
	local char = toolowner.Character or toolowner.CharacterAdded:Wait()
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	local root = char:FindFirstChild("HumanoidRootPart")

	local victim = hitsmth.Parent
	local humofvictim = victim:FindFirstChildOfClass("Humanoid")
	local rootofvictim = victim:FindFirstChild("HumanoidRootPart")

	local ownerstats = char:FindFirstChild("Stats")

	local attmultiplier = (ownerstats and ownerstats:FindFirstChild("AttackMultiplier")) and ownerstats.AttackMultiplier.Value

	local victimstats = victim:FindFirstChild("Stats")
	local victimdefend = (victimstats and victimstats:FindFirstChild("Defensive")) and victimstats.Defensive.Value 

	if not humofvictim or not rootofvictim then return end

	local cmaultrack = humanoid:LoadAnimation(cmaulanim)
	local vmaultrack = humofvictim:LoadAnimation(vmaulanim)

	local victimPlayer = players:GetPlayerFromCharacter(victim)
	if victimPlayer and isSameTeam(toolowner, victimPlayer) then return end

	char:SetAttribute("tanked")

	cmaultrack:Play()
	vmaultrack:Play()

	connection:Disconnect()
	root.Anchored = true
	rootofvictim.Anchored = true

	local pointlight = Instance.new("PointLight")
	pointlight.Color = Color3.new(1, 0, 0)
	pointlight.Parent = rootofvictim
	pointlight.Brightness = 10
	pointlight.Range = 15

	if humofvictim then
		while humofvictim.Health > 0 and humanoid.Health > 0 do
			local damage = damagecal:Calculate(mauldmgper,attmultiplier,victimdefend)
			humofvictim:TakeDamage(damage)
			maul:Play()

			humanoid.AutoRotate = false
			humofvictim.AutoRotate = false
			victim:SetAttribute("CanAttack", false)
			char:SetAttribute("CanAttack", false)

			if root.Anchored ~= true or rootofvictim.Anchored ~= true then
				rootofvictim.Anchored = true
				root.Anchored = true
			end

			dealdmgremote:FireClient(toolowner, victim, damage)

			root.CFrame = CFrame.new(root.Position, Vector3.new(rootofvictim.Position.X, root.Position.Y, rootofvictim.Position.Z))
			rootofvictim.CFrame = root.CFrame * CFrame.new(0, 0, -1)
			rootofvictim.CFrame = CFrame.new(rootofvictim.Position, Vector3.new(root.Position.X, rootofvictim.Position.Y, root.Position.Z))
			task.wait(0.3)

			if humofvictim.Health <= 0 or humanoid.Health <= 0 then

					char:SetAttribute("CanAttack", true)
					char:SetAttribute("tanked", nil)
					victim:SetAttribute("CanAttack", true)
					humanoid.AutoRotate = true
					humofvictim.AutoRotate = true
					rootofvictim.Anchored = false
					root.Anchored = false
					cmaultrack:Stop()
					vmaultrack:Stop()	
					pointlight:Destroy()

				break --//Stops the while loop
			end
		end

	end
end)

this should work.^

respawning? in your video all i see is you spawning the target, you maul them and then you change. Only issue was the animation not stopping? if im correct?

the animation, root part anchoring and huamnoid autorotate, all of them didnt changed when i changed my team(respawned)

its because you aren’t entirely losing health you’re changing your whole character with a setup humanoid of a health > 0.

what you should do is when changing character - set the health on it to 0 when changing on the current then change to the new character u selected then this should stop.

yeah thats right… the main script only handle case when one of them died not changing character