How do I play random song

So I am making a punch tool and wanted to know how to make a randomized punch sound. so If the hitbox hits the other player it plays a random punch sound from a folder inside the handle. How can I do this? this is the script:

local Player = script.Parent.Parent.Parent
local Character = Player.Character
local RaycastHitbox = require(game.ServerScriptService.RaycastHitboxV2)
local ShockWave = require(game.ServerScriptService.Modules.Visualiser.Modules.ShockWave)
local Hitbox = RaycastHitbox:Initialize(script.Parent.Handle)
local DB = false
Hitbox.OnHit:Connect(function(hit, humanoid)
	if humanoid.Parent ~= Character then
		local damage = math.random(5,10)

	local Speed = 25
	local Force = 40000

	local TotalForce = Force

	local KnockBack = Instance.new("BodyVelocity",hit.Parent:FindFirstChild("Torso"))
	ShockWave.MakePart(hit.Position)
	KnockBack.MaxForce = Vector3.new(TotalForce,TotalForce,TotalForce)
	KnockBack.Velocity = Player.Character:FindFirstChild("HumanoidRootPart").CFrame.LookVector * Speed 
	game.Debris:AddItem(KnockBack,0.1)
		spawn(function()
			humanoid.PlatformStand = true
			wait(3)
			humanoid.PlatformStand = false
		end)
	
	humanoid:TakeDamage(damage)
	end
end)
1 Like

You can have a folder with all the punch sounds, get a random value from the children of the folder then play the sound.

local soundTable = script.Sounds:GetChildren()
local sound = soundTable[math.random(1, #soundTable)]
sound:Play()

Or create a sound instance, get a random value from a table with the sound ids, set the sound id and play the sound.

local sound = Instance.new("Sound")
sound.Parent = script

local soundTable = {punchSound1, punchSound2}
sound.SoundId = "rbxassetid://" .. soundTable[math.random(1, #soundTable)]
sound:Play()
8 Likes