Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

attempt to concatenate string with userdata?

Asked by 4 years ago
Edited 4 years ago

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
0
Try removing the space between .. and AudioId Nguyenlegiahung 1091 — 4y
0
Use "," or "+" instead of ".." mixgingengerina10 223 — 4y
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. blarp_blurp645 63 — 4y
0
+ is javascript. nice try tho. Psudar 882 — 4y
0
Ah right, sorry, I usually spend my free time on JS and Python. mixgingengerina10 223 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

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

0
where? maxpax2009 340 — 4y
0
line 12 where the error is changed audioid to my solution ChrisTheRobloxPlayYT 256 — 4y
0
AudioInstance.SoundId = "rbxassetid://" .. tostring(AudioId) -- error is here!! like that ChrisTheRobloxPlayYT 256 — 4y
Ad
Log in to vote
0
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

Answer this question