How to insert a Sound into Workspace using Scripts?

How do I insert a sound into workspace using only scripts?

2 Likes

You would have to instance them in your game. A simple script such as this should do the trick:

local sound = Instance.new(“Sound”)
sound.SoundId = “0”
sound.Parent = game.Workspace

Edit: I will also attach this link which you can also refer to on how sound can be utilized in Roblox.
Link: Sound

3 Likes

local Sound = Instance.new("Sound", workspace)

2 Likes

More of a simplified version than the first.

1 Like

Using the 2nd argument of Instance.new is generally bad practice. Here, it isn’t particularly bad, but if you set properties of the instance, it will hurt performance for no good reason.

1 Like

How will it hurt performance??

1 Like
2 Likes

Wow ok, I’ll remember to use the first method at all times

2 Likes

Set the parent property last but if no other properties are being set then using the parent parameter of the instance class constructor function “Instance.new()” is fine.

3 Likes
function createSound(id, volume, parent)
	assert(id, "sound id wasn't given")
	id = tostring(id):gsub("%D", "") --only keeping the numbers
	assert(id ~= "", "sound id isn't a number") 
	volume = volume or 0.5 --0.5 is the default volume
	parent = parent or workspace --workspace is the default parent
	local Sound = Instance.new("Sound") 
	Sound.SoundId = "rbxassetid://"..id 
	Sound.Volume = volume 
	Sound.Parent = parent 
	return Sound 
end

local Sound = createSound("rbxassetid://1846507211")
Sound.Looped = true 
Sound:Play() 
2 Likes