Sound no longer works in my script

Hello there,

I found out in order for my Tools to work with my Tool Selection UI, they needed to have local scripts inside of them. So I did that, and my tool works, however I do not have any sound in it. Does anyone know how I can fix that?

Code in my script
--[[
	Methods:
	
	1 -	Define: Damages and throws back a target.
		Params: Target humanoid, Damage, Throwback force, Position of origin
	
	2 -	Define: Play a sound.
		Params: Name, Volume, Pitch
	
	3 -	Define: Stop a sound.
		Params: Name
	
	4 -	Define: Stop all sounds.
		Params: N/A
	
	5 - Define: Change the tool's grip properties.
		Params: Position, Rotation
	
	6 - Define: Toggle parry on/off.
		Params: Is on
	
	7 - Define: Send endurance damage to local client.
		Params: Damage
--]]

local tool = script.Parent

local sounds = {}

local function loadSound(passed, name, torso)
	if not torso:findFirstChild(tool.Name.."\\`~`\\"..name) then
		local new = Instance.new("Sound")
		new.SoundId = "rbxassetid://"..passed
		new.Name = tool.Name.."\\`~`\\"..name
		new.Parent = torso
		sounds[name] = new
	end
end

local function checkSounds()
	if tool.Parent and tool.Parent:findFirstChild("Humanoid") and tool.Parent:findFirstChild("Torso") then
		loadSound(211134014, "sheath", tool.Parent.Torso)
		loadSound(211059653, "unsheath", tool.Parent.Torso)
		loadSound(211059855, "clash", tool.Parent.Torso)
		loadSound(186311262, "hit", tool.Parent.Torso)
		loadSound(147722227, "whoosh", tool.Parent.Torso)
	end
end


local function onSE(_, data)
	if data[1] == 1 and data[2] and data[3] and data[4] and data[5] then
		data[2]:TakeDamage(data[3])
		if data[2].Parent and data[2].Parent:findFirstChild("Torso") and data[2].Parent.Torso:IsA("BasePart") and not data[2].Parent.Torso:IsGrounded() then
			data[2].Torso.Velocity = data[2].Torso.Velocity + CFrame.new(data[5], data[2].Torso.Position).lookVector * data[4]
		end
	elseif data[1] == 2 and data[2] and data[3] and data[4] then
		checkSounds()
		if sounds[data[2]] then
			sounds[data[2]].Volume = data[3]
			sounds[data[2]].Pitch = data[4]
			sounds[data[2]]:Play()
		end
	elseif data[1] == 3 and data[2] then
		checkSounds()
		if sounds[data[2]] then
			sounds[data[2]]:Stop()
		end
	elseif data[1] == 4 then
		checkSounds()
		table.foreach(sounds, function(_, sound)
			sound:Stop()
		end)
	elseif data[1] == 5 and data[2] and data[3] then
		tool.GripPos = data[2]
		local _, _, _, rx, ux, fx, ry, uy, fy, rz, uz, fz = CFrame.Angles(math.rad(data[3].X), math.rad(data[3].Y), math.rad(data[3].Z)):components()
		tool.GripRight = Vector3.new(rx, ry, rz)
		tool.GripUp = Vector3.new(ux, uy, uz)
		tool.GripForward = Vector3.new(-fx, -fy, -fz)
	elseif data[1] == 6 and data[2] ~= nil then
		if tool:findFirstChild("ParryPart0") and data[2] then
			tool.ParryPart0.Name = "ParryPart1"
		elseif tool:findFirstChild("ParryPart1") and not data[2] then
			tool.ParryPart1.Name = "ParryPart0"
		end
	elseif data[1] == 7 and data[2] then
		local client = game.Players:GetPlayerFromCharacter(tool.Parent)
		if client then
			script.Comm:FireClient(client, {1, data[2]})
		end
	end
end

game.ReplicatedStorage.Events:WaitForChild("Tool"):connect(onSE)
1 Like

Try returning the Sound Variable after creating the Sound.

local tool = script.Parent

local sounds = {}

local function loadSound(passed, name, torso)
	if not torso:findFirstChild(tool.Name.."\\`~`\\"..name) then
		local new = Instance.new("Sound")
		new.SoundId = "rbxassetid://"..passed
		new.Name = tool.Name.."\\`~`\\"..name
		new.Parent = torso
		sounds[name] = new
		return new
	end
end

local function checkSounds()
	if tool.Parent and tool.Parent:findFirstChild("Humanoid") and tool.Parent:findFirstChild("Torso") then
		sheath = loadSound(211134014, "sheath", tool.Parent.Torso)
		unsheath = loadSound(211059653, "unsheath", tool.Parent.Torso)
		clash = loadSound(211059855, "clash", tool.Parent.Torso)
		hit = loadSound(186311262, "hit", tool.Parent.Torso)
		whoosh = loadSound(147722227, "whoosh", tool.Parent.Torso)
	end
end

This should directly allow you to play the sounds with the variable name.
Like this

local function checkSounds()
	if tool.Parent and tool.Parent:findFirstChild("Humanoid") and tool.Parent:findFirstChild("Torso") then
		sheath = loadSound(211134014, "sheath", tool.Parent.Torso)
		unsheath = loadSound(211059653, "unsheath", tool.Parent.Torso)
		clash = loadSound(211059855, "clash", tool.Parent.Torso)
		hit = loadSound(186311262, "hit", tool.Parent.Torso)
		whoosh = loadSound(147722227, "whoosh", tool.Parent.Torso)
	end
end
whoosh:Play()
sheath:Play()
4 Likes

Thanks man, this helped a lot!

1 Like