Advertisement
HowToRoblox

FoliageHandler

Mar 18th, 2023 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. local char = script.Parent
  2.  
  3. local rnd = Random.new()
  4.  
  5. local foliageTouching = {}
  6.  
  7. local shakeIterations = 0
  8. local shakeSpeed = 0.07
  9. local shakeScale = 0.5
  10.  
  11.  
  12. char.Humanoid.Touched:Connect(function(hit:Instance, bodyPart:Instance)
  13.    
  14.     if hit:FindFirstAncestor("InteractiveFoliage") and not table.find(foliageTouching, hit) then
  15.         table.insert(foliageTouching, hit)
  16.        
  17.         hit.Name = rnd:NextNumber()
  18.        
  19.         if not hit:FindFirstChild("OriginalCFrame") then
  20.             local originalCF = Instance.new("CFrameValue")
  21.             originalCF.Name = "OriginalCFrame"
  22.             originalCF.Value = hit.CFrame
  23.             originalCF.Parent = hit
  24.         end
  25.        
  26.         bodyPart.TouchEnded:Wait()
  27.         table.remove(foliageTouching, table.find(foliageTouching, hit))
  28.     end
  29. end)
  30.  
  31.  
  32. game:GetService("RunService").Heartbeat:Connect(function()
  33.    
  34.     local v = char.HumanoidRootPart.AssemblyLinearVelocity.Magnitude
  35.    
  36.     if v > 0.1 then
  37.         for _, foliage in pairs(foliageTouching) do
  38.             shakeIterations += shakeSpeed
  39.            
  40.             local seed = tonumber(foliage.Name)
  41.  
  42.             local x = math.noise(tick() * shakeSpeed, shakeIterations, seed) * shakeScale
  43.             local y = math.noise(tick() * shakeSpeed, shakeIterations + 1, seed) * shakeScale
  44.             local z = math.noise(tick() * shakeSpeed, shakeIterations + 2, seed) * shakeScale
  45.  
  46.             local newCF = foliage.OriginalCFrame.Value
  47.             newCF += Vector3.new(x, y, z)
  48.             newCF *= CFrame.Angles(x, y, z)
  49.             newCF = foliage.CFrame:Lerp(newCF, 0.05)
  50.  
  51.             foliage.CFrame = newCF
  52.         end
  53.     end
  54. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement