Advertisement
Nuh1xScripter

Realistic Walking in everyone Materials Roblox Studio.

Dec 23rd, 2023
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | Source Code | 0 0
  1. --StarterPlayer >> StarterCharacterScripts >> Create a Script >> Write this >>
  2.  
  3. local players = game:GetService("Players")
  4. local replicatedStorage = game:GetService("ReplicatedStorage")
  5.  
  6. -- Set local variables
  7. local player = players:GetPlayerFromCharacter(script.Parent)
  8. local character = player.Character or player.CharacterAdded:wait()
  9. local humanoid = character.Humanoid
  10.  
  11. -- Create sound variables
  12. local id
  13. local volume
  14. local playbackSpeed
  15.  
  16. -- Create material variables
  17. local floorMaterial
  18. local material
  19.  
  20. -- Create remote instance
  21. local updateWalkspeedRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
  22. updateWalkspeedRemote.Name = "UpdateWalkspeed"
  23.  
  24. -- Create sound instance
  25. local currentSound = Instance.new("Sound", character.HumanoidRootPart)
  26. currentSound.Name = "CurrentSound"
  27. currentSound.RollOffMode = Enum.RollOffMode.Linear -- you can experiment with this value
  28. currentSound.RollOffMinDistance = 10 -- When should the sound start fading out? (in studs)
  29. currentSound.RollOffMaxDistance = 75 -- When should the sound stop being heard? (in studs)
  30.  
  31.  
  32. -- Fetch the ID list
  33. local IDList = require(script:FindFirstChildWhichIsA("ModuleScript"))
  34.  
  35. -- Get the current floor material.
  36. local function getFloorMaterial()
  37. floorMaterial = humanoid.FloorMaterial
  38. material = string.split(tostring(floorMaterial), "Enum.Material.")[2]
  39.  
  40. return material
  41. end
  42.  
  43. -- Get the correct sound from our sound list.
  44. local function getSoundProperties()
  45. for name, data in pairs(IDList) do
  46. if name == material then
  47. id = data.id
  48. volume = data.volume
  49.  
  50. playbackSpeed = (humanoid.WalkSpeed / 16) * data.speed
  51. break
  52. end
  53. end
  54. end
  55.  
  56. -- update the sound data
  57. local function update()
  58. currentSound.SoundId = id
  59. currentSound.Volume = volume
  60. currentSound.PlaybackSpeed = playbackSpeed
  61. end
  62.  
  63. -- Update the previous floor material, current floor material and sound data
  64.  
  65. -- !! BUGGED AS OF THE LATEST ROBLOX UPDATE !!
  66.  
  67. --humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
  68. -- getFloorMaterial()
  69. -- getSoundProperties()
  70. -- update()
  71.  
  72. -- if humanoid.MoveDirection.Magnitude > 0 then
  73. -- currentSound.Playing = true
  74. -- end
  75. --end)
  76.  
  77. -- !! BUGGED AS OF THE LATEST ROBLOX UPDATE !!
  78.  
  79. updateWalkspeedRemote.OnServerEvent:Connect(function(player, walkspeed)
  80. player.Character.Humanoid.WalkSpeed = walkspeed
  81. end)
  82.  
  83. -- check if the player is moving and not climbing
  84. humanoid.Running:Connect(function(speed)
  85. if humanoid.MoveDirection.Magnitude > 0 and speed > 0 and humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then
  86. getSoundProperties()
  87. update()
  88. currentSound.Playing = true
  89. currentSound.Looped = true
  90. else
  91. currentSound:Stop()
  92. end
  93. end)
  94.  
  95. -- Scuffed solution to 'humanoid:GetPropertyChangedSignal('FloorMaterial')' not working as expected on the server.
  96. spawn(function()
  97. while task.wait() do
  98. if humanoid.FloorMaterial ~= floorMaterial then
  99. getFloorMaterial()
  100. getSoundProperties()
  101. update()
  102. end
  103. end
  104. end)
  105.  
  106. print("Walking sounds successfully loaded!")
  107.  
  108. --Your script >> press "+" >> Add a LocalScript >> Write This >>
  109.  
  110. local players = game:GetService("Players")
  111. local soundService = game:GetService("SoundService")
  112. local replicatedStorage = game:GetService("ReplicatedStorage")
  113.  
  114. -- Set local variables
  115. local player = players.LocalPlayer
  116. local character = player.Character or player.CharacterAdded:wait()
  117. local humanoid = character.Humanoid
  118.  
  119. -- Create sound variables
  120. local id
  121. local volume
  122. local playbackSpeed
  123.  
  124. -- Create material variables
  125. local floorMaterial
  126. local material
  127.  
  128. -- Get the HumanoidRootPart and wait for the server audio
  129. local root = character:WaitForChild("HumanoidRootPart")
  130. local serverSound = root:WaitForChild("CurrentSound")
  131.  
  132. -- Get remote
  133. local updateWalkspeedRemote = replicatedStorage:WaitForChild("UpdateWalkspeed")
  134.  
  135. -- Create sound instance
  136. local currentSound = Instance.new("Sound", soundService)
  137. currentSound.Name = "CurrentSound"
  138.  
  139. -- fetch the ID list
  140. local IDList = require(script.Parent:FindFirstChildWhichIsA("ModuleScript"))
  141.  
  142. -- Delete default running sound
  143. character.HumanoidRootPart:FindFirstChild("Running"):Destroy()
  144.  
  145. -- Get the current floor material.
  146. local function getFloorMaterial()
  147. floorMaterial = humanoid.FloorMaterial
  148. material = string.split(tostring(floorMaterial), "Enum.Material.")[2]
  149.  
  150. return material
  151. end
  152.  
  153. -- Get the correct sound from our sound list.
  154. local function getSoundProperties()
  155. for name, data in pairs(IDList) do
  156. if name == material then
  157. id = data.id
  158. volume = data.volume
  159. playbackSpeed = (humanoid.WalkSpeed / 16) * data.speed
  160. break
  161. end
  162. end
  163. end
  164.  
  165. -- update the sound data
  166. local function update()
  167. currentSound.SoundId = id
  168. currentSound.Volume = volume
  169. currentSound.PlaybackSpeed = playbackSpeed
  170. end
  171.  
  172. -- Get initial data for client
  173. getFloorMaterial()
  174. getSoundProperties()
  175. update()
  176.  
  177. -- Update the previous floor material and current floor material
  178. humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
  179. getFloorMaterial()
  180. getSoundProperties()
  181. update()
  182.  
  183. if humanoid.MoveDirection.Magnitude > 0 then
  184. currentSound.Playing = true
  185. end
  186. end)
  187.  
  188. -- Let the server know our walkspeed has changed
  189. humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
  190. updateWalkspeedRemote:FireServer(humanoid.WalkSpeed)
  191. end)
  192.  
  193. -- check if the player is moving and not climbing
  194. humanoid.Running:Connect(function(speed)
  195. if humanoid.MoveDirection.Magnitude > 0 and speed > 0 and humanoid:GetState() ~= Enum.HumanoidStateType.Climbing then
  196. getSoundProperties()
  197. update()
  198. currentSound.Playing = true
  199. currentSound.Looped = true
  200. else
  201. currentSound:Stop()
  202. end
  203. end)
  204.  
  205. serverSound.Changed:Connect(function(Volume)
  206. if Volume ~= 0 then
  207. serverSound.Volume = 0
  208. end
  209. end)
  210.  
  211. -- Small bug fix where the sound would start playing after the player joined
  212. player.CharacterAdded:Connect(function()
  213. task.wait(1)
  214. if currentSound.IsPlaying then
  215. currentSound:Stop()
  216. end
  217. end)
  218.  
  219. print("Walking sounds successfully loaded!")
  220.  
  221. --In the first Script created >> press "+" >> Add a ModuleScript >> Write This >>
  222.  
  223. local IDList = {
  224. Air = {id = "rbxassetid://329997777", volume = 0, speed = 1.00},
  225. Asphalt = {id = "rbxassetid://277067660", volume = 0.60, speed = 1.00},
  226. Basalt = {id = "rbxassetid://3190903775", volume = 0.60, speed = 1.00},
  227. Brick = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  228. Cobblestone = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  229. Concrete = {id = "rbxassetid://277067660", volume = 0.60, speed = 1.00},
  230. CorrodedMetal = {id = "rbxassetid://177940974", volume = 0.60, speed = 1.00},
  231. CrackedLava = {id = "rbxassetid://3190903775", volume = 0.60, speed = 1.00},
  232. DiamondPlate = {id = "rbxassetid://177940974", volume = 0.60, speed = 1.00},
  233. Fabric = {id = "rbxassetid://9083849830", volume = 0.40, speed = 1.00},
  234. Foil = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  235. Forcefield = {id = "rbxassetid://329997777", volume = 0.60, speed = 1.00},
  236. Glass = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  237. Granite = {id = "rbxassetid://178054124", volume = 0.60, speed = 1.00},
  238. Grass = {id = "rbxassetid://9064714296", volume = 0.60, speed = 1.00},
  239. Glacier = {id = "rbxassetid://7047108275", volume = 0.40, speed = 1.00},
  240. Ground = {id = "rbxassetid://9064714296", volume = 0.60, speed = 1.00},
  241. Ice = {id = "rbxassetid://7047108275", volume = 0.40, speed = 1.00},
  242. Limestone = {id = "rbxassetid://9083846829", volume = 0.60, speed = 1.00},
  243. LeafyGrass = {id = "rbxassetid://3098847639", volume = 0.60, speed = 1.00},
  244. Marble = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  245. Metal = {id = "rbxassetid://177940974", volume = 0.60, speed = 1.00},
  246. Mud = {id = "rbxassetid://6441160246", volume = 0.60, speed = 1.00},
  247. Neon = {id = "rbxassetid://177940974", volume = 0.60, speed = 1.00},
  248. Pebble = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  249. Plastic = {id = "rbxassetid://4416041299", volume = 0.60, speed = 1.40},
  250. Pavement = {id = "rbxassetid://277067660", volume = 0.60, speed = 1.00},
  251. Rock = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  252. Sand = {id = "rbxassetid://9083846829", volume = 0.40, speed = 1.00},
  253. Slate = {id = "rbxassetid://178054124", volume = 0.60, speed = 1.00},
  254. Snow = {id = "rbxassetid://8453425942", volume = 0.60, speed = 1.00},
  255. Salt = {id = "rbxassetid://9083846829", volume = 0.40, speed = 1.00},
  256. Sandstone = {id = "rbxassetid://3190903775", volume = 0.60, speed = 0.75},
  257. SmoothPlastic = {id = "rbxassetid://178190837", volume = 0.60, speed = 1.00},
  258. Wood = {id = "rbxassetid://3199270096", volume = 0.60, speed = 1.00},
  259. WoodPlanks = {id = "rbxassetid://211987063", volume = 0.60, speed = 1.00}
  260. }
  261.  
  262. return IDList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement