Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would i use math.random to randomize dictionaries?

I have been trying to see what I am doing wrong for about 3 hours and I don't know what Its doing so ill just ask here. No the code is not mine I found it on the dev form but i wanted to use it. what I am trying to do is to make the sounds on each floor play different sound effects (1-4 different ones) but I cant for the life of me figure it out. So if you can figure it out and help me that would be great! (And if you could explain how it would actually work that would help me a lot!)

local players = game:GetService"Players"
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild"Humanoid"
if not humanoid.RootPart then humanoid:GetPropertyChangedSignal"RootPart":Wait() end
local root = humanoid.RootPart
local running = root:WaitForChild("Running")

local materialSounds = {
    ["Air"] = "rbxassetid://7616579820", "rbxassetid://7616579820", "rbxassetid://7616579820", "rbxassetid://7616579820",
    ["Metal"] = "rbxassetid://9120589933", "rbxassetid://9120584645", "rbxassetid://685857471", "rbxassetid://9117946851",
    ["Plastic"] = "rbxassetid://7574087626", "rbxassetid://7574083547", "rbxassetid://7574087626", "rbxassetid://7574083547", 

}

local function onFloorMaterialChanged()
    if humanoid.FloorMaterial then
        running.SoundId = materialSounds[humanoid.FloorMaterial.Name]
    else
        running.SoundId = "rbxasset://379483672"
    end
end

local floorMaterialChanged = humanoid:GetPropertyChangedSignal"FloorMaterial"
floorMaterialChanged:Connect(onFloorMaterialChanged)

1 answer

The solution is to store tables of soundIds inside each item of the dictionary.

Then, when it picks a sound, you do:

--for example
sound.SoundId = materialSounds["Air"][math.random(1,#materialSounds["Air"])]
sound:Play()

--storing tables:
local dictionary = {
  ["Item"] = {"SoundId1","SoundId2","SoundId3"},
}

hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.