What do you want to achieve? I want to make sounds loop at a constant rate when firing a gun. I know games like Phantom Forces and Arsenal are able to do this, but we’re having a hard time getting around issues.
What is the issue? Sound seems to stutter every few second when played in a loop.
What solutions have you tried so far? I’ve tried creating new instances of the sound each shot and playing the new instances, using the :Play() method on the same one sound each shot. Either way, there’s stuttering every few seconds.
-- basic example of looped sound stuttering
for i=0, math.huge do
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://4827208824"
sound.PlayOnRemove = true
sound:Destroy()
wait(0.08) -- Fire rate of gun
end
There can be two reasons. REASON 1: The sound is longer than the fire rate. REASON 2: The sound id does not load in right away. ← If this is the case I would not make a new sound but to clone a sound from replicatedstorage, or the workspace, etc.
This was just a demonstration to show how looped sounds will stutter. Our actual code is pretty similar, we just check to see if the left click button is being held down, then we fire a shot, play the gun sound, wait for a given amount of time, then repeat the previous steps. As long as the left click button is held down, the gun fires in what is equivalent to a loop. I thought using a loop would look cleaner, and it has the exact same stuttering issue as our real code.
I tried your suggestion, but I still get stuttering. Here’s my code:
sound.SoundId = "rbxassetid://4827208824"
while true do
local copy = sound:Clone()
copy.PlayOnRemove = true
copy.Parent = script.Parent
copy:Destroy()
wait(0.08) -- Fire rate of gun
end
I think so. You are trying to play that sound every 0.08 seconds and the sound is 2 seconds long. So if you click play after 0.08 seconds the sound goes back to 0 seconds and restart itself. Edit: Sorry I am bad at english.