Why is this not working

dont KNOW why its NOT WORKING

local UserInputService = game:GetService("UserInputService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local Sound = nil

UserInputService.InputBegan:Connect(function(InputBegan)
	if InputBegan.UserInputType == Enum.UserInputType.MouseButton1 then
		Sound = Instance.new("Sound")
		Sound.Name = HttpService:GenerateGUID()
		Sound.SoundId = "http://roblox.com/asset/?id=873129141"
		Sound.Parent = Players.LocalPlayer.Character
		if not Sound.IsPlaying then
			Sound:Play()
		end
	end
end)

I GET NO ERRORS AT ALL

ITS IN A LOCALSCRIPT

PLEASE TELL ME A FIX FOR IT

1 Like

Any errors? Can you be specific on what you’re trying to do

Create a sound effect when I left click and not play another one until the present one has completed!

Did any errors occur? It may be with your if statements.

If it’s not working, can you specify the error?

Send us a screenshot of your Output Window.

can you update the title and desc. to be more clear, don’t want anyone confused.

No one is very sure what the problem is.

I’ve corrected the code below. Your issue was that you were creating a new sound every time you clicked. As a result, when you clicked again a new sound was created that wasn’t playing yet.

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local Sound = Instance.new("Sound")
Sound.Name = HttpService:GenerateGUID()
Sound.SoundId = "http://roblox.com/asset/?id=873129141"
Sound.Parent = Players.LocalPlayer.Character

UserInputService.InputBegan:Connect(function(InputBegan)
	if InputBegan.UserInputType == Enum.UserInputType.MouseButton1 then
		if not Sound.IsPlaying then
			Sound:Play()
		end
	end
end)
4 Likes

You’ve been at this long enough to know to give more information. What debugging steps have you tried? Where does the code get to? Is input being detected, is it the mouse button, is the sound even created, or is there a sound but no audio. We don’t magically fix problems, give us details when you get stuck but you are the first step in debugging, and you need to take some steps before you consult us. You also need to mark solutions and share your answers because it is very important to a community built around answering these questions and to avoid future questions of the same nature.

2 Likes

Yes I know I can do that but I want to create a new sound everytime I click

Ok, I need you to redo the title and description to day what your actual problem is.

Then mark @CyMule’s post as a solution, so people in the future don’t need to ask (thst is very important)

Then we can help, people will flag this if you don’t make it more descriptive, then the topic will be taken down.

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local Sound = Instance.new("Sound")
Sound.Name = HttpService:GenerateGUID()
Sound.SoundId = "http://roblox.com/asset/?id=873129141"
Sound.Parent = Players

UserInputService.InputBegan:Connect(function(InputBegan)
	if InputBegan.UserInputType == Enum.UserInputType.MouseButton1 then
		if not Sound.IsPlaying then
			Sound:Play()
		end
	end
end)
1 Like

Try something like this:

local UserInputService = game:GetService("UserInputService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")

local playing = false

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if not playing then
            playing = true

            local sound = Instance.new("Sound")
            sound.Name = HttpService:GenerateGUID()
            sound.SoundId = "rbxassetid://873129141"
            sound.Parent = Players.LocalPlayer
            sound:Play()

            wait(sound.TimeLength)
            sound:Destroy()

            playing = false
        end
    end
end)
1 Like

Sound is play but to many times at once I get no erros

Allows me to shoot once and that is it

You might need to add a debounce

Someone tried that but did not owkr

@xDeltaXen what are you wanting to achieve

I have already stated what I want to ahieve

I gun sound affect that does not allow you to spam play it

stop it spamming my using Sound.IsPlaying

THAT is literally MY CODE like bruhhh

I it isnt i changed something try it

1 Like