Advertisement
coolden300

Untitled

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