Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i use math.random to randomize dictionaries?

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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.

Ad

Answer this question