Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to concatenate string with userdata?

I made a music, playing script, but it does not concatenate properly? The error is attempt to concatenate string with userdata.

local AudioList = {
["ThereItIs"] = 279498364,
["FrostWalts"] = 2037145146,
["LocalForecast"] = 258852901,
["Decisions"] = 337055476,
["Run"] = 1470017017,
["Preposition"] = 257383979,
}
local ContentProvider = game:GetService("ContentProvider")
for name, AudioId in pairs(AudioList) do
	local AudioInstance = Instance.new("Sound")
	AudioInstance.SoundId = "rbxassetid://" .. AudioId -- error is here!!
	AudioInstance.Volume = 0.1
	AudioInstance.Name = name
	AudioInstance.Parent = game.Workspace
	table.insert(AudioList, AudioInstance)
end

5 comments

0

Try removing the space between .. and AudioId

0

Use "," or "+" instead of ".."

0

I've dealt with a lot of these errors in my own games. I don't know the specific problem here (I'll try to look more into it), but basically what the problem was with mine was that the variable it's trying to use is a userdata value, not a string. It may look like a string, but it's actually not. The way I fixed this was by changing the value to the value's name, so it'll become a string value.

0
  • is javascript. nice try tho.
0

Ah right, sorry, I usually spend my free time on JS and Python.

3 answers

The only thing i can think of right now is adding a tostring(AudioId) instead of the raw AudioId so theyre both a string. Try that

3 comments

0

where?

0

line 12 where the error is changed audioid to my solution

0

AudioInstance.SoundId = "rbxassetid://" .. tostring(AudioId) -- error is here!! like that

It's because you're inserting the audio instance into the table. The table you're looping through will also get updated. Try using a new table or something, the problem is this line though:

table.insert(AudioList, AudioInstance)

I'd do something like this:

local ContentProvider = game:GetService("ContentProvider")

local AudioList = {
["ThereItIs"] = 279498364,
["FrostWalts"] = 2037145146,
["LocalForecast"] = 258852901,
["Decisions"] = 337055476,
["Run"] = 1470017017,
["Preposition"] = 257383979,
}

local audioFiles = {}

for name, AudioId in pairs(AudioList) do
    local AudioInstance = Instance.new("Sound")
    AudioInstance.SoundId = "rbxassetid://" .. AudioId -- error is here!!
    AudioInstance.Volume = 0.1
    AudioInstance.Name = name
    AudioInstance.Parent = game.Workspace
    audioFiles[#audioFiles + 1] = audioInstance
end

This should work, just like ChrisTheRobloxPlayYT said, use tostring()

Code:

local AudioList = {
	["ThereItIs"] = 279498364,
	["FrostWalts"] = 2037145146,
	["LocalForecast"] = 258852901,
	["Decisions"] = 337055476,
	["Run"] = 1470017017,
	["Preposition"] = 257383979,
}
local ContentProvider = game:GetService("ContentProvider")
for name, Id in pairs(AudioList) do
    local AudioInstance = Instance.new("Sound")
    AudioInstance.SoundId = 'rbxassetid://' .. tostring(Id) -- error is here!!
    AudioInstance.Volume = 0.1
    AudioInstance.Name = name
    AudioInstance.Parent = game.Workspace
    table.insert(AudioList, AudioInstance)
end

But to prevent creation of the same song multiple times we add it to another table like Psudar said

local AudioList = {
    ["ThereItIs"] = 279498364,
    ["FrostWalts"] = 2037145146,
    ["LocalForecast"] = 258852901,
    ["Decisions"] = 337055476,
    ["Run"] = 1470017017,
    ["Preposition"] = 257383979,
}
local ContentProvider = game:GetService("ContentProvider")

local addedAudio = {}

for name, Id in pairs(AudioList) do
    local AudioInstance = Instance.new("Sound")
    AudioInstance.SoundId = 'rbxassetid://' .. tostring(Id) -- error is here!!
    AudioInstance.Volume = 0.1
    AudioInstance.Name = name
    AudioInstance.Parent = game.Workspace
    table.insert(addedAudio, AudioInstance)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.