I’ve made a katana and i want to make it play a random sound when it hits something.
How would i go about making a table to choose a random sound from it?
4 Likes
local handle = script.Parent
local sound1 = handle:WaitForChild("Sound1")
local sound2 = handle:WaitForChild("Sound2")
local sounds = {sound1, sound2}
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
sounds[math.random(1, #sounds)]:Play()
end
end)
9 Likes
local handle = script.Parent
local sounds = {}
for i, child in pairs(handle:GetChildren()) do
if child:IsA("Sound") then
table.insert(sounds, child)
end
end
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
sounds[math.random(1, #sounds)]:Play()
end
end)
As long as the script is placed inside the handle of the tool with the “Sound” instances you want to pick randomly from these will both work.