Making a tool that plays a random sound everytime

I’m currently trying to make a tool that plays a random sound every time it is clicked; however, I am having trouble making that possible. Every time the tool is activated I get the error

Failed to load sound rbxassetid://3: Unable to download sound data (x2)

Here is my script for reference. any help is appreciated
ServerScript

local BatHittingSounds = {
	175024455,
	3932505023,
	3755636638
}
RemoteEvents.ServerGameFunction.OnServerEvent:Connect(function(Player, Event, Table)
	if Event == "BatActivated" then
		local BatSound = Instance.new("Sound")
		BatSound.Parent = workspace
		BatSound.SoundId = "rbxassetid://"..math.random(1, #BatHittingSounds)
		BatSound:Play()
		wait(BatSound.TimeLength)
		BatSound:Stop()
	end
end)

Have you tried looking anywhere?

2 Likes

Hiya, what do you mean by that?

Have you searched for this topic or in this problem

1 Like

Yeah, I have; I haven’t found the solution to my problem

I don’t know why I feel like you don’t have a problem

1 Like

I mean the script doesn’t work, so I do have one.

Your math.random is picking a number between 1 and the number of entries in the table. Instead, try this:

local BatHittingSounds = {
	175024455,
	3932505023,
	3755636638
}
RemoteEvents.ServerGameFunction.OnServerEvent:Connect(function(Player, Event, Table)
	if Event == "BatActivated" then
		local BatSound = Instance.new("Sound")
		BatSound.Parent = workspace
		BatSound.SoundId = "rbxassetid://"..BatHittingSounds[math.random(1, #BatHittingSounds)]
		BatSound:Play()
		wait(BatSound.TimeLength)
		BatSound:Stop()
	end
end)

Thank you so much; I knew I had made a stupid mistake.

I’m sorry, I can’t help you Honestly, your problem is Western.

1 Like

Hiya also; instead of workspace I’m trying make the sound’s parent be the players head. How would I do that? I’ve tried putting it through a function but that didn’t seem to work.

local BatHittingSounds = {
	175024455,
	3932505023,
	3755636638
}

local head
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		head = character:FindFirstChild("Head")
	end)
end)

RemoteEvents.ServerGameFunction.OnServerEvent:Connect(function(Player, Event, Table)
	if Event == "BatActivated" then
		local BatSound = Instance.new("Sound")
		BatSound.Parent = head
		BatSound.SoundId = "rbxassetid://"..math.random(1, #BatHittingSounds)
		BatSound:Play()
		wait(BatSound.TimeLength)
		BatSound:Stop()
	end
end)

Nothing happens. No Outputs either.

local BatHittingSounds = {
	175024455,
	3932505023,
	3755636638
}

RemoteEvents.ServerGameFunction.OnServerEvent:Connect(function(Player, Event, Table)
	if Event == "BatActivated" then
		local head = Player.Character:FindFirstChild("Head")
		local BatSound = Instance.new("Sound")
		BatSound.Parent = head
		BatSound.SoundId = "rbxassetid://"..BatHittingSounds[math.random(1, #BatHittingSounds)]
		BatSound:Play()
		wait(BatSound.TimeLength)
		BatSound:Stop()
	end
end)
1 Like

image

Rewrote the code, should be fixed now

Thank you so much; it works now, I really appreciate it.

1 Like

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