Help with script where you press G and it makes a sound

Title

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local function playsound()
	local AudioPlayer = require(ReplicatedStorage:WaitForChild("AudioPlayer"))

	AudioPlayer.setupAudio({
		["click2down"] = 6052548458
	})
end
UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.G then
			local function playsound()
		
				
			end	
		end
	end
end)

script is in StarterPlayerScripts
audioPlayer script:

local SoundService = game:GetService("SoundService")

local AudioPlayer = {}

AudioPlayer.setupAudio = function(assetArray)
	for name, audioID in pairs(assetArray) do
		local audioInstance = Instance.new("Sound")
		audioInstance.SoundId = "rbxassetid://"..6052548458
		audioInstance.SoundId = "rbxassetid://"..12563989892
		audioInstance.Name = name
		audioInstance.Parent = SoundService
	end
end

AudioPlayer.playAudio = function(assetName)
	local audio = SoundService:FindFirstChild(assetName)
	if audio then
		if not audio.IsLoaded then
			audio.Loaded:Wait()
		end
		audio:Play()
	end
end


return AudioPlayer
3 Likes

wait i cant tell, are you even playing the audio?? with the AudioPlayer.playAudio???

2 Likes

When the InputBegan event gets fired, you’re overloading the “playsound” function, basically replacing the function’s code

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.G then
			playsound()
		end
	end
end)

yes, just read the script lmao

1 Like

It just looks like you’re setting the audio up but not playing it

1 Like

All good! I do this all the time :joy:

1 Like

So now, i have to make a variable for the key G, so then i can link that variable to play the sound.

Yes, you probably could just plug the sounds ID into the G key so it sends it to set it up

Don’t need to declare the function here, just called the function… playsound()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.