Audio script puts wrong Sound Id in wrong audio

Title, there is one Module Script, that I use to play sounds when you hover or press a Text Button; however, it uses the wrong sound id

I will show a few scripts for context:

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.SoundId = "rbxassetid://"..6895079853
		audioInstance.SoundId = "rbxassetid://"..552900451
		
		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

here’s one of the scripts i use for playing the sounds in the button:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AudioPlayer = require(ReplicatedStorage:WaitForChild("AudioPlayer"))

AudioPlayer.setupAudio({
	["button_click"] = 552900451
})

local textBUtton = script.Parent

textBUtton.Activated:Connect(function()
	AudioPlayer.playAudio("button_click")
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AudioPlayer = require(ReplicatedStorage:WaitForChild("AudioPlayer"))

AudioPlayer.setupAudio({
	["uiHoverClick"] = 6895079853
})


local button = script.Parent

button.MouseEnter:Connect(function()
	AudioPlayer.playAudio("uiHoverClick")
end)
button.MouseLeave:Connect(function()
	AudioPlayer.playAudio("uiHoverClick")
end)

Sound Ids for both just incase: uiHoverClick = 6895079853
button_click = 552900451

Looks fine right? Unfortunely, this happens:

Look; the uiHoverClick has the same soundId as Button_Click.
Please help me fix this issue, as I have no idea why this is occuring.

In the module script, you are setting the soundId of each audio to the same thing each time instead of its key in the dictionary.

Replace

audioInstance.SoundId = "rbxassetid://"..6052548458
audioInstance.SoundId = "rbxassetid://"..12563989892
audioInstance.SoundId = "rbxassetid://"..6895079853
audioInstance.SoundId = "rbxassetid://"..552900451

With

audioInstance.SoundId = "rbxassetid://" .. tostring(audioID)
1 Like

Hey! Tried your method, still didn’t work; did you mean tonumber instead?

If you use tonumber() then Roblox will just convert it to a string because you are trying to add a number to a string.

put print(audio) in the playaudio function to check if it was found

1 Like

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