Is it not playing because the animation isn’t working, or your code isn’t running, use a print statement.
I have the animation inside the NPC rig, You meant that?
Load the animation using LoadAnimation()
:
local surrenderAnimation = npc.Humanoid.Animator:LoadAnimation(makeSurrender) surrenderAnimation:Play()
May i ask where should i replace or should i add?
Write it like this:
local npc = workspace.NPC
local makeSurrender = npc:FindFirstChild("SurrenderAnimation"):LoadAnimation()
if makeSurrender then
makeSurrender:Play()
else
print("SurrenderAnimation not found!")
end
It won’t work if you didn’t publish the animation, or you don’t own it.
I own the animation, i did actually publish it. And it didn’t work.
This is what the function should look like.
local function makeSurrender()
local npc = workspace.NPC
local makeSurrender = npc:FindFirstChild("SurrenderAnimation")
local humanoid = npc:FindFirstChild("Humanoid")
if makeSurrender and humanoid then
local surrenderAnim = humanoid.Animator:LoadAnimation(makeSurrender)
surrenderAnim:Play()
else
print("SurrenderAnimation not found!")
end
end
It didn’t work unfortianately. (I’m sorry if i spell something incorrectly i’m hungarian and it is a bit hard to speak english.)
Does it print “SurrenderAnimation not found!” or is there an error?
It doesn’t print that, console doesn’t show any errors, same as the script.
That means that the function is not being called, which means that you messed up somewhere here:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function(Health)
if Health < 90 then
makeSurrender()
end
end)
end)
end)
Did you change the humanoid.HealthChanged:Connect()
to what @Den_vers stated?: :GetPropertyChangedSignal("Health"):Connect()
I actually did.
(30 thing bla bla)
Here is the correct script.
local function makeSurrender()
local npc = workspace.NPC
local makeSurrender = npc:FindFirstChild("SurrenderAnimation")
local humanoid = npc:FindFirstChild("Humanoid")
if makeSurrender and humanoid then
local surrenderAnim = humanoid.Animator:LoadAnimation(makeSurrender)
surrenderAnim:Play()
else
print("SurrenderAnimation not found!")
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
local Health = humanoid.Health
if Health < 90 then
makeSurrender()
end
end)
end)
end)
The issue I am seeing here, is first that you are not loading the animation properly. You must load it somewhat like this.
local function makeSurrender()
local npc = workspace:FindFirstChild("NPC")
local makeSurrenderAnim = -- [[Path to your animation instance for now it's nil]]
nil
if makeSurrenderAnim ~= nil then
local makeSurrenderAnimTrack = npc:FindFirstChildOfClass("Humanoid").Animator:LoadAnimation(makeSurrenderAnim)
makeSurrenderAnimTrack:Play()
end
end
Also you are checking the player health and then making the NPC surrender which doesn’t make sense, so if you’re trying to make the NPC surrender at a certain health amount you need to connect the event to the NPC’s humanoid, not the player’s. Unless the player is getting hurt the NPC will not surrender in this instance.
EDIT: Adding documentation
Animator | Documentation - Roblox Creator Hub
AnimationTrack | Documentation - Roblox Creator Hub
Ontop of @tannnxr 's post, you should add debounce to the surrender function because it’s going to trigger more than once if there’s regen enabled.
Also, where should i put the script? I used to leave this script inside the rig’s model
I recommend you give this a read:
Please also read the #help-and-feedback:scripting-support template before creating a post.
Well, if you’re going to do it off of the player’s health for some reason, put it in ServerScriptService. If not, yeah, put it inside of the NPC and rewrite the code to fit the NPC’s health instead of the player’s.
I appreciate you linked my topic, but I recommend you help the person first and then link it after they find a solution so they’ll remember it for future topics. It’ll be more helpful that way .
idk how the health works but if i was you id use humanoid.health < 90