Hello, I was wondering how I would to make a Random sounds playing each time a player dies. Last time I made a script of that but it was only was playing to everyone’s device and not playing from the dead character location.
2 Likes
Try this
local m = math.random(1,5)
And then use if statement
1 Like
To play a script globally, that is, so all players can hear it, you must execute your code in a server script.
Try something like this:
local function main()
-- Table of our sounds to use later
local sounds : any = {}
-- Iterate through the folder where the sounds are kept
for k : number, v : Sound in pairs(sounds_folder_or_model) do
-- Equality check that the currently iterated instance is a sound
if v:IsA("Sound") then
-- Append the currently iterated instance to our sounds table
table.append(sounds, v)
end
end
-- Pick a random entry between 1 and the length of our sounds table
-- (the number of sounds we have) and play it
sounds[math.random(1, #sounds)]:Play()
end
-- Never-ending loops are bad practice, but this is an example
while true do
main()
task.wait(1)
end
Hope this helps.
When the player dies, you can clone a random sound object from your folder of death sounds then parent it to their character’s humanoid root part (you could also parent the sound into a part then parent that to the humanoid root part’s position if their character gets destroyed after death).
An example might look something like this:
--*inside starter character scripts*
local Char = script.Parent
local SoundFolder = --path to your death sounds folder
local Sounds = SoundFolder:GetChildren()
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
Humanoid.Died:Connect(function()
local ChosenSound = Sounds[math.random(1,#Sounds)]:Clone()
ChosenSound.Parent = HumanoidRootPart
ChosenSound:Play()
end)
And that should do the trick!
4 Likes
Thanks! That worked out pretty good.
1 Like
If you want to SLIENCE the normal (and terrifying ) sound, edit the code to this:
--*inside starter character scripts*
local Char = script.Parent
local SoundFolder = --path to your death sounds folder
local Sounds = SoundFolder:GetChildren()
local Humanoid = Char:WaitForChild("Humanoid")
local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
Humanoid.Died:Connect(function()
local ChosenSound = Sounds[math.random(1,#Sounds)]:Clone()
ChosenSound.Parent = HumanoidRootPart
ChosenSound:Play()
HumanoidRootPart:WaitForChild("Died"):Destroy()
end)
2 Likes