Guest User

viewmodel for roblox

a guest
Jan 25th, 2025
5,359
2
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 2 0
  1. local RunService = game:GetService("RunService")
  2.  
  3. local viewModelsFolder = game.ReplicatedStorage.ViewModels
  4.  
  5. local camera = game.Workspace.CurrentCamera
  6.  
  7. local player = game.Players.LocalPlayer
  8. local char = player.Character or player.CharacterAdded:Wait()
  9.  
  10. -------------- settings -----------------------
  11.  
  12. --are on the models, in the Configuration in the ViewModels folder (ReplicatedStorage)
  13.  
  14. local smooth = 50
  15.  
  16. local bobSpeed = 1.5
  17. local bobDistance = .1
  18. local bobWalkMulti = 3
  19. local walkVeloThreshold = 9
  20.  
  21. local swaySpeed = 1.1
  22. local swayDistance = .07
  23. local swayWalkMulti = 2
  24.  
  25. -----------------------------------------------
  26.  
  27. local tool = nil
  28. local viewModel = nil
  29.  
  30. local targetCFrame = CFrame.new()
  31.  
  32. function GetTargetCFrame()
  33. local bobPos
  34. local swayPos
  35. if char.PrimaryPart.AssemblyLinearVelocity.Magnitude >= walkVeloThreshold then
  36. bobPos = math.sin(tick() * (bobSpeed * bobWalkMulti)) * bobDistance
  37. swayPos = math.sin(tick() * (swaySpeed * swayWalkMulti)) * swayDistance
  38. else
  39. bobPos = math.sin(tick() * bobSpeed) * bobDistance
  40. swayPos = math.sin(tick() * swaySpeed) * swayDistance
  41. end
  42.  
  43. return camera.CFrame * viewModel.Configuration:GetAttribute("Offset") + Vector3.new(0, bobPos, swayPos)
  44. end
  45.  
  46. char.DescendantAdded:Connect(function(t)
  47. --checking for a tool to be added to the plr
  48.  
  49. if t:IsA("Tool") and t ~= nil then
  50.  
  51. tool = t
  52. viewModel = viewModelsFolder[tool.Name]:Clone()
  53. viewModel.Parent = camera
  54. viewModel.Name = viewModel.Name .. " viewmodel"
  55. --set the inital position for the viewmodel
  56. viewModel.PrimaryPart:PivotTo(GetTargetCFrame() * CFrame.new(0, -2, 0))
  57.  
  58. for _, p in ipairs(t:GetDescendants()) do
  59. if p:IsA("BasePart") then
  60. p.LocalTransparencyModifier = 1
  61.  
  62. elseif p:IsA("SurfaceLight") then
  63. p.Brightness = 0
  64. end
  65. end
  66. end
  67. end)
  68.  
  69. char.DescendantRemoving:Connect(function(t)
  70. if t == tool then
  71. tool = nil
  72. viewModel:Destroy()
  73. viewModel = nil
  74. end
  75. end)
  76.  
  77. RunService.RenderStepped:Connect(function(deltaTime)
  78. if viewModel then
  79. targetCFrame = GetTargetCFrame()
  80.  
  81. viewModel.PrimaryPart.CFrame = viewModel.PrimaryPart.CFrame:Lerp(targetCFrame, deltaTime * smooth)
  82. end
  83. end)
Advertisement
Add Comment
Please, Sign In to add comment