Animation playing on wrong character?

Hi, im doing a bg-kind of “Attack” and, i want it to make so when a hitbox is touched by another character, the player character and the victim character plays diffrent animations, so i got it, but for some reason my character plays the victim anim, and the victim character doesnt do anything, here is my script:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local db = false

script.Parent.Equipped:Connect(function()
	if not db then
		db = true
		hum:UnequipTools(script.Parent)
		local charRs = game.ReplicatedStorage.Characters.TestChar
		local newHitbox = charRs.Punch.Hitbox:Clone()
		local dmg = 30
		local cdTime = 1
		local success = false
		local hitted = false
		local hitboxTime = .5
		local anim = Instance.new("Animation")
		anim.AnimationId = script.PlrAnim.AnimationId
		local plrTrack = hum:LoadAnimation(anim)
		local startAnim = Instance.new("Animation")
		startAnim.AnimationId = script.Start.AnimationId
		local start = hum:LoadAnimation(startAnim)
		
		newHitbox.Parent = workspace
		newHitbox.CFrame = char:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -6)
		newHitbox.Touched:Connect(function(hit)
			if not hitted then
				success = true
				hitted = true
				local victimChar = hit.Parent
				local victimHum = victimChar:FindFirstChild("Humanoid")
				local victimAnim = Instance.new("Animation")
				victimAnim.AnimationId = script.VictimAnim.AnimationId
				local victimTrack = victimHum:LoadAnimation(victimAnim)
				if victimChar.Name ~= char.Name then
					print(victimChar.Name, char.Name)
					print("Working")
					victimTrack:Play()
					victimChar:MoveTo(newHitbox.CFrame)
					victimHum.Health -= 30
				else
					print("Own char")
				end
			end
		end)
		if success then
			startAnim:Play()
			wait(hitboxTime)
			newHitbox:Destroy()
		else
			local failAnim = Instance.new("Animation")
			failAnim.AnimationId = script.Fail.AnimationId
			local fail = hum:LoadAnimation(failAnim)
			fail:Play()
			wait(hitboxTime)
			newHitbox:Destroy()
		end

		wait(cdTime - hitboxTime)
		db = false
	else
		hum:UnequipTools(script.Parent)
	end	
end)
1 Like

Is the newHitbox hitting the attacker’s character by any chance?

yeah, and for some reason when i touch the hitbox it plays the animation, and btw i just found out that it doesnt play the anim if i dont touch it, but as i said the victim character wont play it

When the hitbox is touched, add an if statement to make sure “hit” isn’t a descendant of the attacker character. If it’s still not hitting the “victim”, try adding a print statement inside the .Touched to check the hit part and its parent.

so i did some changes and it was a lil of an error, but can you check again the code, i alr edited it, the console wont show anything exept if i touch it, wich will say “own char”

You need to add the if statement in front of the line that sets hitted = true. Since you’re hitting your own character before you hit the “victim”, hitted is set to true and you won’t be able to deal damage/play the animation on the “victim”.

like this?

newHitbox.Touched:Connect(function(hit)
			if not hitted then
				success = true
				hitted = true
				if hit:IsDescendantOf(char) then
					print("Own plr")
				else
					local victimChar = hit.Parent
					local victimHum = victimChar:FindFirstChild("Humanoid")
					local victimAnim = Instance.new("Animation")
					victimAnim.AnimationId = script.VictimAnim.AnimationId
					local victimTrack = victimHum:LoadAnimation(victimAnim)
					if victimChar.Name ~= char.Name then
						print(victimChar.Name, char.Name)
						print("Working")
						victimTrack:Play()
						victimChar:MoveTo(newHitbox.CFrame)
						victimHum.Health -= 30
					else
						print("Own char")
					end
				end
			end

btw it didnt work

No , you are setting hitted to true before doing the check. You want to do the check either outside of the if not hitted then, or before the hitted = true.

if hit:IsDescendantOf(char) then
					print("Own plr")
				else
					success = true
					hitted = true
					local victimChar = hit.Parent
					local victimHum = victimChar:FindFirstChild("Humanoid")
					local victimAnim = Instance.new("Animation")
					victimAnim.AnimationId = script.VictimAnim.AnimationId
					local victimTrack = victimHum:LoadAnimation(victimAnim)
					if victimChar.Name ~= char.Name then
						print(victimChar.Name, char.Name)
						print("Working")
						victimTrack:Play()
						victimChar:MoveTo(newHitbox.CFrame)
						victimHum.Health -= 30
					else
						print("Own char")
					end
				end

didnt work neither…

What prints? Try adding a print statement under the first else.

i did it, but it wont print anything

by the way, i fixed it, i just made a hitlist, idk how to explain it so here is the code:

hitList = {}
--rest of code until Touched()
touched:Connect(function(hit)
table.insert(hitList, char)
table.insert(hitList, hit.Parent)
if hit.Parent ~= hitList[1] then
--etc...
end
end)

But anyways thanks!!

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