Advertisement
HowToRoblox

WalkHandler

Jan 20th, 2023
1,666
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 1 0
  1. local sounds = game:GetService("ReplicatedStorage"):WaitForChild("FootstepsReplicatedStorage"):WaitForChild("Sounds")
  2. local footstep = game:GetService("ReplicatedStorage").FootstepsReplicatedStorage:WaitForChild("Footstep"):WaitForChild("FootstepPart")
  3.  
  4.  
  5. local footstepsContainer = Instance.new("Folder")
  6. footstepsContainer.Name = "FOOTSTEPS"
  7. footstepsContainer.Parent = workspace
  8.  
  9.  
  10. local ts = game:GetService("TweenService")
  11. local fadeTI = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
  12.  
  13.  
  14. local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
  15. local hum = char:WaitForChild("Humanoid")
  16.  
  17. local root = char:WaitForChild("HumanoidRootPart")
  18. root:WaitForChild("Running").Volume = 0
  19.  
  20. local walkAnim = hum:WaitForChild("Animator"):LoadAnimation(script:WaitForChild("Animation"))
  21.  
  22. local notWalkingStates = {Enum.HumanoidStateType.Jumping, Enum.HumanoidStateType.FallingDown, Enum.HumanoidStateType.Climbing, Enum.HumanoidStateType.Swimming, Enum.HumanoidStateType.Flying, Enum.HumanoidStateType.Freefall, Enum.HumanoidStateType.Landed}
  23.  
  24.  
  25. local footstepMaterials = {
  26.     "Sand",
  27.     "Mud",
  28.     "Grass",
  29. }
  30.  
  31. local timeForStepToDisappear = 5
  32.  
  33. local colorOffset = Color3.new(-0.1, -0.1, -0.1)
  34.  
  35.  
  36. game:GetService("RunService").Heartbeat:Connect(function()
  37.    
  38.     local md = hum.MoveDirection.Magnitude
  39.     local currentState = hum:GetState()
  40.    
  41.     walkAnim:AdjustSpeed(hum.WalkSpeed / 16)
  42.    
  43.     if md > 0.1 and not walkAnim.IsPlaying and not table.find(notWalkingStates, currentState) then
  44.         walkAnim:Play()
  45.  
  46.     elseif (md <= 0.1 or table.find(notWalkingStates, currentState)) and walkAnim.IsPlaying then
  47.         walkAnim:Stop()
  48.     end
  49. end)
  50.  
  51.  
  52. function getPartUnderCharacter(foot)
  53.    
  54.     local origin = foot.Position + Vector3.new(0, 0.5, 0)
  55.     local direction = Vector3.new(0, -1, 0)
  56.    
  57.     local rp = RaycastParams.new()
  58.     rp.FilterDescendantsInstances = {char, footstepsContainer}
  59.     rp.IgnoreWater = true
  60.    
  61.     local ray = workspace:Raycast(origin, direction, rp)
  62.    
  63.     if ray and ray.Instance.Transparency < 1 then
  64.        
  65.         local material = string.split(tostring(ray.Material), ".")[3]
  66.        
  67.         local sound = sounds:FindFirstChild(material) or sounds:WaitForChild("Default")
  68.        
  69.         local soundCopy = sound:Clone()
  70.         soundCopy.Parent = foot
  71.         soundCopy:Play()
  72.        
  73.         soundCopy.Ended:Connect(function()
  74.             soundCopy:Destroy()
  75.         end)
  76.        
  77.         if ray.Instance.Anchored and table.find(footstepMaterials, material) then
  78.            
  79.             local newFootstep = footstep:Clone()
  80.            
  81.             local position = foot.Position + Vector3.new(0, newFootstep.Size.Y/2 - foot.Size.Y/2, 0)
  82.             local orientation = Vector3.new(0, foot.Orientation.Y, 0) + Vector3.new(ray.Normal.Z, 0, ray.Normal.X)
  83.            
  84.             newFootstep.CFrame = CFrame.new(position) * CFrame.Angles(orientation.X, math.rad(orientation.Y), orientation.Z)
  85.            
  86.             newFootstep.Material = ray.Instance.Material
  87.            
  88.             newFootstep.Transparency = ray.Instance.Transparency
  89.            
  90.             local r = math.clamp(ray.Instance.Color.R - colorOffset.R, 0, 1)
  91.             local g = math.clamp(ray.Instance.Color.G - colorOffset.G, 0, 1)
  92.             local b = math.clamp(ray.Instance.Color.B - colorOffset.B, 0, 1)
  93.             newFootstep.Color = Color3.new(r, g, b)
  94.            
  95.             newFootstep.Parent = footstepsContainer
  96.            
  97.             task.wait(timeForStepToDisappear)
  98.            
  99.             local fadeTween = ts:Create(newFootstep, fadeTI, {Transparency = 1})
  100.             fadeTween:Play()
  101.            
  102.             fadeTween.Completed:Wait()
  103.             fadeTween:Destroy()
  104.             newFootstep:Destroy()
  105.         end
  106.     end
  107. end
  108.  
  109.  
  110. walkAnim:GetMarkerReachedSignal("Right Foot Down"):Connect(function()
  111.     getPartUnderCharacter(char:WaitForChild("RightFoot"))
  112. end)
  113. walkAnim:GetMarkerReachedSignal("Left Foot Down"):Connect(function()
  114.     getPartUnderCharacter(char:WaitForChild("LeftFoot"))
  115. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement