I want the playback speed to be changed after every click (math.random() ) [Raycast SMG]

ok so im making this SMG and to prevent the Gunshot sound to be the same i want it to be speed from
0.8,1
script:

local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local muzzle = script.Parent
local fired = tool:WaitForChild("Shotsfired")
local sound = tool:WaitForChild("Sound")
tool.Equipped:Connect(function(mouse)
    fired.Value = 1
    print("DesertEagle Equipped!")
     wait(1)

	mouse.Button1Down:Connect(function()
        print("Mouse pressed!")
		script.Parent.Parent.Sound.PlaybackSpeed = math.random(0.8,1)
		script.Parent.Parent.Sound:Play()
        local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p - muzzle.CFrame.p).unit * 100)
        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
 
        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Daisy orange")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.45
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false
 
        local distance = (muzzle.CFrame.p - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(muzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
 
        game:GetService("Debris"):AddItem(beam, 0.1)
 
        if part then
            local humanoid = part.Parent:FindFirstChild("Humanoid")
 
            if not humanoid then
                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            end
 		
            if humanoid then
                wait(0.5)
                humanoid:TakeDamage(35)
            
				end
			end
	end)
end)

it seems to either be playing it, or not at all now, and after some time of rapid fire it stopped rendering for debugging, oh and how could i make it shoot when the LMB is held

1 Like

For making it shoot while the lmb is held, thats just making a value true and false when mouse.Button1Down and Mouse.Button1Up and having a while ‘bool’ do loop with the firerate inside the mouse.Button1Down

For the sound it seems like you’re using a different sound than the one in your locals? Check output to make sure theres nothing wrong.

1 Like

Good idea withthe value true thing, but how can i even determite if the player is holding it, Button1Down aswell? And all the output says is nothing i think, buti can check again later

haha yes, you got 0.5% of my request covered up, thx bro :slight_smile:
but now the sounds are cut off, i would like the sounds to always finish, not just break up cause its restarting
oh and ye you didnt help me with anything about the sound part yet, cause all of the local variables are correct

local tool = script.Parent.Parent
local player = game:GetService("Players").LocalPlayer
local muzzle = script.Parent
local IsShooting = tool:WaitForChild("IsShooting")
local sound = tool:WaitForChild("Sound")
tool.Equipped:Connect(function(mouse)
    print("DesertEagle Equipped!")
     wait(1)

	mouse.Button1Down:Connect(function()
        print("Mouse pressed!")
		IsShooting.Value = true
		
			while IsShooting.Value == true do
				wait(0.1)	
					script.Parent.Parent.Sound.PlaybackSpeed = math.random(0.8,1)			
					script.Parent.Parent.Sound:Play()       
					local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p - muzzle.CFrame.p).unit * 100)
        			local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)	
        			local beam = Instance.new("Part", workspace)
        			beam.BrickColor = BrickColor.new("Daisy orange")
        			beam.FormFactor = "Custom"
        			beam.Material = "Neon"
        			beam.Transparency = 0.45
        			beam.Anchored = true
        			beam.Locked = true
        			beam.CanCollide = false
     				local distance = (muzzle.CFrame.p - position).magnitude
        			beam.Size = Vector3.new(0.3, 0.3, distance)
        			beam.CFrame = CFrame.new(muzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
        			game:GetService("Debris"):AddItem(beam, 0.1)
        			if part then
            			local humanoid = part.Parent:FindFirstChild("Humanoid")
 
            		if not humanoid then
                		humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
            		end
				
            		if humanoid then
                		wait(0.5)
                		humanoid:TakeDamage(35)
    				end
	
	mouse.Button1Up:Connect(function()
			print("Button1Released")
			IsShooting.Value = false	
	end)
			end
		end
	end)
	end)

reread what i said as i already said it is “it seems to either be playing it, or not at all now, and after some time of rapid fire it stopped rendering for debugging, oh and how could i make it shoot when the LMB is held”

“ok so im making this SMG and to prevent the Gunshot sound to be the same i want it to be speed from
0.8,”

You gotta clone the gunshot sound and then destroy it after its done playing if you wanna do that.

1 Like

oh, good idea30characterssssssss

anyway, but it is still either playing a sound, or none at all “math.random(0.8,1)”

Maybe try Random.new():NextNumber(0.8,1)

1 Like

Gonna try that tomortow, thanks in advance :slight_smile:

ok so what happened now is it played the sound 3x, then stopped playing or even stopped shooting then i noticed its playback speed changes from 1 to 0 each time its played
but what happened, theres no bad output either

edit:
Ok sometimes it just doesnt loop at all an donly gets to play the sound and doesnt get to print mouse button released and i have to spam LMB, what is wrong here?!

2nd edit: oh and sometimes it firstly requires me to spam LMB, then after some while it starts getting me to hold LMB and release it, but then stops doing anything else…

oh and i forgot to post updated script
´´´
local tool = script.Parent.Parent
local player = game:GetService(“Players”).LocalPlayer
local muzzle = script.Parent
local IsShooting = tool:WaitForChild(“IsShooting”)
local sound = tool:WaitForChild(“Sound”)
local soundclone = sound:Clone()
tool.Equipped:Connect(function(mouse)
print(“DesertEagle Equipped!”)
wait(1)

mouse.Button1Down:Connect(function()
    print("Mouse pressed!")
	IsShooting.Value = true
	script.Parent.Parent.Sound.PlaybackSpeed = math.random(0.8,1)
		while IsShooting.Value == true do
			wait(0.1)	
				soundclone.Parent = script.Parent.Parent
				sound:Play()
				sound.Ended:Wait()
				sound:Destroy()
				local ray = Ray.new(muzzle.CFrame.p, (mouse.Hit.p - muzzle.CFrame.p).unit * 100)
    			local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)	
    			local beam = Instance.new("Part", workspace)
    			beam.BrickColor = BrickColor.new("Daisy orange")
    			beam.FormFactor = "Custom"
    			beam.Material = "Neon"
    			beam.Transparency = 0.45
    			beam.Anchored = true
    			beam.Locked = true
    			beam.CanCollide = false
 				local distance = (muzzle.CFrame.p - position).magnitude
    			beam.Size = Vector3.new(0.3, 0.3, distance)
    			beam.CFrame = CFrame.new(muzzle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
    			game:GetService("Debris"):AddItem(beam, 0.1)
    			if part then
        			local humanoid = part.Parent:FindFirstChild("Humanoid")

        		if not humanoid then
            		humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        		end
			
        		if humanoid then
            		wait(0.5)
            		humanoid:TakeDamage(35)
				end

mouse.Button1Up:Connect(function()
		print("Button1Released")
		IsShooting.Value = false	
end)
		end
	end
end)
end)

3rd problem is that the script’s local, meaning it only kills the other player locally…