Advertisement
mrmrcoder

R6 NPC animate script server

Jun 3rd, 2024
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.65 KB | None | 0 0
  1. -- humanoidAnimatePlayEmote.lua
  2.  
  3. local Figure = script.Parent
  4. local Torso = Figure:WaitForChild("Torso")
  5. local RightShoulder = Torso:WaitForChild("Right Shoulder")
  6. local LeftShoulder = Torso:WaitForChild("Left Shoulder")
  7. local RightHip = Torso:WaitForChild("Right Hip")
  8. local LeftHip = Torso:WaitForChild("Left Hip")
  9. local Neck = Torso:WaitForChild("Neck")
  10. local Humanoid = Figure:WaitForChild("Humanoid")
  11. local pose = "Standing"
  12.  
  13. local EMOTE_TRANSITION_TIME = 0.1
  14.  
  15. local userAnimateScaleRunSuccess, userAnimateScaleRunValue = pcall(function() return UserSettings():IsUserFeatureEnabled("UserAnimateScaleRun") end)
  16. local userAnimateScaleRun = userAnimateScaleRunSuccess and userAnimateScaleRunValue
  17.  
  18. local function getRigScale()
  19.     if userAnimateScaleRun then
  20.         return Figure:GetScale()
  21.     else
  22.         return 1
  23.     end
  24. end
  25.  
  26. local currentAnim = ""
  27. local currentAnimInstance = nil
  28. local currentAnimTrack = nil
  29. local currentAnimKeyframeHandler = nil
  30. local currentAnimSpeed = 1.0
  31. local animTable = {}
  32. local animNames = {
  33.     idle =  {  
  34.         { id = "http://www.roblox.com/asset/?id=180435571", weight = 9 },
  35.         { id = "http://www.roblox.com/asset/?id=180435792", weight = 1 }
  36.     },
  37.     walk =  {  
  38.         { id = "http://www.roblox.com/asset/?id=180426354", weight = 10 }
  39.     },
  40.     run =   {
  41.         { id = "run.xml", weight = 10 }
  42.     },
  43.     jump =  {
  44.         { id = "http://www.roblox.com/asset/?id=125750702", weight = 10 }
  45.     },
  46.     fall =  {
  47.         { id = "http://www.roblox.com/asset/?id=180436148", weight = 10 }
  48.     },
  49.     climb = {
  50.         { id = "http://www.roblox.com/asset/?id=180436334", weight = 10 }
  51.     },
  52.     sit =   {
  53.         { id = "http://www.roblox.com/asset/?id=178130996", weight = 10 }
  54.     }, 
  55.     toolnone = {
  56.         { id = "http://www.roblox.com/asset/?id=182393478", weight = 10 }
  57.     },
  58.     toolslash = {
  59.         { id = "http://www.roblox.com/asset/?id=129967390", weight = 10 }
  60.         --              { id = "slash.xml", weight = 10 }
  61.     },
  62.     toollunge = {
  63.         { id = "http://www.roblox.com/asset/?id=129967478", weight = 10 }
  64.     },
  65.     wave = {
  66.         { id = "http://www.roblox.com/asset/?id=128777973", weight = 10 }
  67.     },
  68.     point = {
  69.         { id = "http://www.roblox.com/asset/?id=128853357", weight = 10 }
  70.     },
  71.     dance1 = {
  72.         { id = "http://www.roblox.com/asset/?id=182435998", weight = 10 },
  73.         { id = "http://www.roblox.com/asset/?id=182491037", weight = 10 },
  74.         { id = "http://www.roblox.com/asset/?id=182491065", weight = 10 }
  75.     },
  76.     dance2 = {
  77.         { id = "http://www.roblox.com/asset/?id=182436842", weight = 10 },
  78.         { id = "http://www.roblox.com/asset/?id=182491248", weight = 10 },
  79.         { id = "http://www.roblox.com/asset/?id=182491277", weight = 10 }
  80.     },
  81.     dance3 = {
  82.         { id = "http://www.roblox.com/asset/?id=182436935", weight = 10 },
  83.         { id = "http://www.roblox.com/asset/?id=182491368", weight = 10 },
  84.         { id = "http://www.roblox.com/asset/?id=182491423", weight = 10 }
  85.     },
  86.     laugh = {
  87.         { id = "http://www.roblox.com/asset/?id=129423131", weight = 10 }
  88.     },
  89.     cheer = {
  90.         { id = "http://www.roblox.com/asset/?id=129423030", weight = 10 }
  91.     },
  92. }
  93. local dances = {"dance1", "dance2", "dance3"}
  94.  
  95. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  96. local emoteNames = { wave = false, point = false, dance1 = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  97.  
  98. function configureAnimationSet(name, fileList)
  99.     if (animTable[name] ~= nil) then
  100.         for _, connection in pairs(animTable[name].connections) do
  101.             connection:disconnect()
  102.         end
  103.     end
  104.     animTable[name] = {}
  105.     animTable[name].count = 0
  106.     animTable[name].totalWeight = 0
  107.     animTable[name].connections = {}
  108.  
  109.     -- check for config values
  110.     local config = script:FindFirstChild(name)
  111.     if (config ~= nil) then
  112.         --      print("Loading anims " .. name)
  113.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  114.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  115.         local idx = 1
  116.         for _, childPart in pairs(config:GetChildren()) do
  117.             if (childPart:IsA("Animation")) then
  118.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  119.                 animTable[name][idx] = {}
  120.                 animTable[name][idx].anim = childPart
  121.                 local weightObject = childPart:FindFirstChild("Weight")
  122.                 if (weightObject == nil) then
  123.                     animTable[name][idx].weight = 1
  124.                 else
  125.                     animTable[name][idx].weight = weightObject.Value
  126.                 end
  127.                 animTable[name].count = animTable[name].count + 1
  128.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  129.                 --          print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
  130.                 idx = idx + 1
  131.             end
  132.         end
  133.     end
  134.  
  135.     -- fallback to defaults
  136.     if (animTable[name].count <= 0) then
  137.         for idx, anim in pairs(fileList) do
  138.             animTable[name][idx] = {}
  139.             animTable[name][idx].anim = Instance.new("Animation")
  140.             animTable[name][idx].anim.Name = name
  141.             animTable[name][idx].anim.AnimationId = anim.id
  142.             animTable[name][idx].weight = anim.weight
  143.             animTable[name].count = animTable[name].count + 1
  144.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  145.             --          print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  146.         end
  147.     end
  148. end
  149.  
  150. -- Setup animation objects
  151. function scriptChildModified(child)
  152.     local fileList = animNames[child.Name]
  153.     if (fileList ~= nil) then
  154.         configureAnimationSet(child.Name, fileList)
  155.     end
  156. end
  157.  
  158. script.ChildAdded:connect(scriptChildModified)
  159. script.ChildRemoved:connect(scriptChildModified)
  160.  
  161. -- Clear any existing animation tracks
  162. -- Fixes issue with characters that are moved in and out of the Workspace accumulating tracks
  163. local animator = if Humanoid then Humanoid:FindFirstChildOfClass("Animator") else nil
  164. if animator then
  165.     local animTracks = animator:GetPlayingAnimationTracks()
  166.     for i,track in ipairs(animTracks) do
  167.         track:Stop(0)
  168.         track:Destroy()
  169.     end
  170. end
  171.  
  172.  
  173. for name, fileList in pairs(animNames) do
  174.     configureAnimationSet(name, fileList)
  175. end
  176.  
  177. -- ANIMATION
  178.  
  179. -- declarations
  180. local toolAnim = "None"
  181. local toolAnimTime = 0
  182.  
  183. local jumpAnimTime = 0
  184. local jumpAnimDuration = 0.3
  185.  
  186. local toolTransitionTime = 0.1
  187. local fallTransitionTime = 0.3
  188. local jumpMaxLimbVelocity = 0.75
  189.  
  190. -- functions
  191.  
  192. function stopAllAnimations()
  193.     local oldAnim = currentAnim
  194.  
  195.     -- return to idle if finishing an emote
  196.     if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  197.         oldAnim = "idle"
  198.     end
  199.  
  200.     currentAnim = ""
  201.     currentAnimInstance = nil
  202.     if (currentAnimKeyframeHandler ~= nil) then
  203.         currentAnimKeyframeHandler:disconnect()
  204.     end
  205.  
  206.     if (currentAnimTrack ~= nil) then
  207.         currentAnimTrack:Stop()
  208.         currentAnimTrack:Destroy()
  209.         currentAnimTrack = nil
  210.     end
  211.     return oldAnim
  212. end
  213.  
  214. function setAnimationSpeed(speed)
  215.     if speed ~= currentAnimSpeed then
  216.         currentAnimSpeed = speed
  217.         currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  218.     end
  219. end
  220.  
  221. function keyFrameReachedFunc(frameName)
  222.     if (frameName == "End") then
  223.  
  224.         local repeatAnim = currentAnim
  225.         -- return to idle if finishing an emote
  226.         if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  227.             repeatAnim = "idle"
  228.         end
  229.  
  230.         local animSpeed = currentAnimSpeed
  231.         playAnimation(repeatAnim, 0.0, Humanoid)
  232.         setAnimationSpeed(animSpeed)
  233.     end
  234. end
  235.  
  236. -- Preload animations
  237. function playAnimation(animName, transitionTime, humanoid)
  238.  
  239.     local roll = math.random(1, animTable[animName].totalWeight)
  240.     local origRoll = roll
  241.     local idx = 1
  242.     while (roll > animTable[animName][idx].weight) do
  243.         roll = roll - animTable[animName][idx].weight
  244.         idx = idx + 1
  245.     end
  246.     --      print(animName .. " " .. idx .. " [" .. origRoll .. "]")
  247.     local anim = animTable[animName][idx].anim
  248.  
  249.     -- switch animation    
  250.     if (anim ~= currentAnimInstance) then
  251.  
  252.         if (currentAnimTrack ~= nil) then
  253.             currentAnimTrack:Stop(transitionTime)
  254.             currentAnimTrack:Destroy()
  255.         end
  256.  
  257.         currentAnimSpeed = 1.0
  258.  
  259.         -- load it to the humanoid; get AnimationTrack
  260.         currentAnimTrack = humanoid:LoadAnimation(anim)
  261.         currentAnimTrack.Priority = Enum.AnimationPriority.Core
  262.  
  263.         -- play the animation
  264.         currentAnimTrack:Play(transitionTime)
  265.         currentAnim = animName
  266.         currentAnimInstance = anim
  267.  
  268.         -- set up keyframe name triggers
  269.         if (currentAnimKeyframeHandler ~= nil) then
  270.             currentAnimKeyframeHandler:disconnect()
  271.         end
  272.         currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  273.  
  274.     end
  275.  
  276. end
  277.  
  278. -------------------------------------------------------------------------------------------
  279. -------------------------------------------------------------------------------------------
  280.  
  281. local toolAnimName = ""
  282. local toolAnimTrack = nil
  283. local toolAnimInstance = nil
  284. local currentToolAnimKeyframeHandler = nil
  285.  
  286. function toolKeyFrameReachedFunc(frameName)
  287.     if (frameName == "End") then
  288.         --      print("Keyframe : ".. frameName)   
  289.         playToolAnimation(toolAnimName, 0.0, Humanoid)
  290.     end
  291. end
  292.  
  293.  
  294. function playToolAnimation(animName, transitionTime, humanoid, priority)     
  295.  
  296.     local roll = math.random(1, animTable[animName].totalWeight)
  297.     local origRoll = roll
  298.     local idx = 1
  299.     while (roll > animTable[animName][idx].weight) do
  300.         roll = roll - animTable[animName][idx].weight
  301.         idx = idx + 1
  302.     end
  303.     --      print(animName .. " * " .. idx .. " [" .. origRoll .. "]")
  304.     local anim = animTable[animName][idx].anim
  305.  
  306.     if (toolAnimInstance ~= anim) then
  307.  
  308.         if (toolAnimTrack ~= nil) then
  309.             toolAnimTrack:Stop()
  310.             toolAnimTrack:Destroy()
  311.             transitionTime = 0
  312.         end
  313.  
  314.         -- load it to the humanoid; get AnimationTrack
  315.         toolAnimTrack = humanoid:LoadAnimation(anim)
  316.         if priority then
  317.             toolAnimTrack.Priority = priority
  318.         end
  319.  
  320.         -- play the animation
  321.         toolAnimTrack:Play(transitionTime)
  322.         toolAnimName = animName
  323.         toolAnimInstance = anim
  324.  
  325.         currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  326.     end
  327. end
  328.  
  329. function stopToolAnimations()
  330.     local oldAnim = toolAnimName
  331.  
  332.     if (currentToolAnimKeyframeHandler ~= nil) then
  333.         currentToolAnimKeyframeHandler:disconnect()
  334.     end
  335.  
  336.     toolAnimName = ""
  337.     toolAnimInstance = nil
  338.     if (toolAnimTrack ~= nil) then
  339.         toolAnimTrack:Stop()
  340.         toolAnimTrack:Destroy()
  341.         toolAnimTrack = nil
  342.     end
  343.  
  344.  
  345.     return oldAnim
  346. end
  347.  
  348. -------------------------------------------------------------------------------------------
  349. -------------------------------------------------------------------------------------------
  350.  
  351.  
  352. function onRunning(speed)
  353.     speed /= getRigScale()
  354.  
  355.     if speed > 0.01 then
  356.         playAnimation("walk", 0.1, Humanoid)
  357.         if currentAnimInstance and currentAnimInstance.AnimationId == "http://www.roblox.com/asset/?id=180426354" then
  358.             setAnimationSpeed(speed / 14.5)
  359.         end
  360.         pose = "Running"
  361.     else
  362.         if emoteNames[currentAnim] == nil then
  363.             playAnimation("idle", 0.1, Humanoid)
  364.             pose = "Standing"
  365.         end
  366.     end
  367. end
  368.  
  369. function onDied()
  370.     pose = "Dead"
  371. end
  372.  
  373. function onJumping()
  374.     playAnimation("jump", 0.1, Humanoid)
  375.     jumpAnimTime = jumpAnimDuration
  376.     pose = "Jumping"
  377. end
  378.  
  379. function onClimbing(speed)
  380.     speed /= getRigScale()
  381.  
  382.     playAnimation("climb", 0.1, Humanoid)
  383.     setAnimationSpeed(speed / 12.0)
  384.     pose = "Climbing"
  385. end
  386.  
  387. function onGettingUp()
  388.     pose = "GettingUp"
  389. end
  390.  
  391. function onFreeFall()
  392.     if (jumpAnimTime <= 0) then
  393.         playAnimation("fall", fallTransitionTime, Humanoid)
  394.     end
  395.     pose = "FreeFall"
  396. end
  397.  
  398. function onFallingDown()
  399.     pose = "FallingDown"
  400. end
  401.  
  402. function onSeated()
  403.     pose = "Seated"
  404. end
  405.  
  406. function onPlatformStanding()
  407.     pose = "PlatformStanding"
  408. end
  409.  
  410. function onSwimming(speed)
  411.     if speed > 0 then
  412.         pose = "Running"
  413.     else
  414.         pose = "Standing"
  415.     end
  416. end
  417.  
  418. function getTool() 
  419.     for _, kid in ipairs(Figure:GetChildren()) do
  420.         if kid.className == "Tool" then return kid end
  421.     end
  422.     return nil
  423. end
  424.  
  425. function getToolAnim(tool)
  426.     for _, c in ipairs(tool:GetChildren()) do
  427.         if c.Name == "toolanim" and c.className == "StringValue" then
  428.             return c
  429.         end
  430.     end
  431.     return nil
  432. end
  433.  
  434. function animateTool()
  435.  
  436.     if (toolAnim == "None") then
  437.         playToolAnimation("toolnone", toolTransitionTime, Humanoid, Enum.AnimationPriority.Idle)
  438.         return
  439.     end
  440.  
  441.     if (toolAnim == "Slash") then
  442.         playToolAnimation("toolslash", 0, Humanoid, Enum.AnimationPriority.Action)
  443.         return
  444.     end
  445.  
  446.     if (toolAnim == "Lunge") then
  447.         playToolAnimation("toollunge", 0, Humanoid, Enum.AnimationPriority.Action)
  448.         return
  449.     end
  450. end
  451.  
  452. function moveSit()
  453.     RightShoulder.MaxVelocity = 0.15
  454.     LeftShoulder.MaxVelocity = 0.15
  455.     RightShoulder:SetDesiredAngle(3.14 /2)
  456.     LeftShoulder:SetDesiredAngle(-3.14 /2)
  457.     RightHip:SetDesiredAngle(3.14 /2)
  458.     LeftHip:SetDesiredAngle(-3.14 /2)
  459. end
  460.  
  461. local lastTick = 0
  462.  
  463. function move(time)
  464.     local amplitude = 1
  465.     local frequency = 1
  466.     local deltaTime = time - lastTick
  467.     lastTick = time
  468.  
  469.     local climbFudge = 0
  470.     local setAngles = false
  471.  
  472.     if (jumpAnimTime > 0) then
  473.         jumpAnimTime = jumpAnimTime - deltaTime
  474.     end
  475.  
  476.     if (pose == "FreeFall" and jumpAnimTime <= 0) then
  477.         playAnimation("fall", fallTransitionTime, Humanoid)
  478.     elseif (pose == "Seated") then
  479.         playAnimation("sit", 0.5, Humanoid)
  480.         return
  481.     elseif (pose == "Running") then
  482.         playAnimation("walk", 0.1, Humanoid)
  483.     elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  484.         --      print("Wha " .. pose)
  485.         stopAllAnimations()
  486.         amplitude = 0.1
  487.         frequency = 1
  488.         setAngles = true
  489.     end
  490.  
  491.     if (setAngles) then
  492.         local desiredAngle = amplitude * math.sin(time * frequency)
  493.  
  494.         RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
  495.         LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
  496.         RightHip:SetDesiredAngle(-desiredAngle)
  497.         LeftHip:SetDesiredAngle(-desiredAngle)
  498.     end
  499.  
  500.     -- Tool Animation handling
  501.     local tool = getTool()
  502.     if tool and tool:FindFirstChild("Handle") then
  503.  
  504.         local animStringValueObject = getToolAnim(tool)
  505.  
  506.         if animStringValueObject then
  507.             toolAnim = animStringValueObject.Value
  508.             -- message recieved, delete StringValue
  509.             animStringValueObject.Parent = nil
  510.             toolAnimTime = time + .3
  511.         end
  512.  
  513.         if time > toolAnimTime then
  514.             toolAnimTime = 0
  515.             toolAnim = "None"
  516.         end
  517.  
  518.         animateTool()      
  519.     else
  520.         stopToolAnimations()
  521.         toolAnim = "None"
  522.         toolAnimInstance = nil
  523.         toolAnimTime = 0
  524.     end
  525. end
  526.  
  527. -- connect events
  528. Humanoid.Died:connect(onDied)
  529. Humanoid.Running:connect(onRunning)
  530. Humanoid.Jumping:connect(onJumping)
  531. Humanoid.Climbing:connect(onClimbing)
  532. Humanoid.GettingUp:connect(onGettingUp)
  533. Humanoid.FreeFalling:connect(onFreeFall)
  534. Humanoid.FallingDown:connect(onFallingDown)
  535. Humanoid.Seated:connect(onSeated)
  536. Humanoid.PlatformStanding:connect(onPlatformStanding)
  537. Humanoid.Swimming:connect(onSwimming)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement