Kind of relating to my last post, I’m trying to make a table of sounds for when a character dies. The LocalScript is in (should be) the correct spot, in StarterPlayer > StarterPlayerScripts. Here’s the script so far:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local Players = game:GetService("Players")
local Humanoid = char:WaitForChild("Humanoid")
local DeathSounds = game.Workspace.DeathsFolder:GetChildren()
local newlocation = DeathSounds.Parent == game.Workspace:WaitForChild("" ..player.Name).HumanoidRootPart
local randomSound = newlocation.DeathSounds[math.random(1, #newlocation.DeathSounds)]
local death1 = newlocation.JackeryzDeath1
local death2 = newlocation.JackeryzDeath2
local death3 = newlocation.JackeryzDeath3
Humanoid.Died:Connect(function()
randomSound:Play()
if death1.Playing == true then
wait(.29)
death1:Stop()
death1.TimePosition = 1.34
end
if death2.Playing == true then
wait(.27)
death2:Stop()
death2.TimePosition = 2.64
end
if death3.Playing == true then
wait(.25)
death3:Stop()
death3.TimePosition = 4.55
end
end)
end)
end)
There are no errors or anything, so I’m not sure what’s going wrong. so ya i need help!!!
This wouldn’t work though because the local’s for the death1,2,3 are going to be in the player.Name’s HumanoidRootPart, and the player part is only is the part of the code where the PlayerAdded is. Sorry if I explained that poorly.
Ok, I’ve figured out one way on way this isn’t working as well.
The folder (local DeathSounds) doesn’t move over to the HumanoidRootPart. I know this is totally out of the blue but I would not know why it’s doing this as I’ve done this in other scripts before.
There’s no point in defining a new variable if you don’t use it. If you just want to change the location of the folder to the HumanoidRootPart then just do:
-- // Services
local Players = game:GetService("Players")
-- // Variables
local DeathSounds = {
{Id = "", WaitTime = 0.29, TimePosition = 1.34},
{Id = "", WaitTime = 0.27, TimePosition = 2.64},
{Id = "", WaitTime = 0.25, TimePosition = 4.55},
}
-- // Functions
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
-- Set the random seed based on os.time()
math.randomseed(math.random(-os.time(), os.time()))
-- Generate random number and get the sound
local randomNumber = math.random(1, #DeathSounds)
local randomSound = DeathSounds[randomNumber]
humanoid.Died:Connect(function()
-- Loop through all objects in the DeathSounds array
for index, sound in ipairs(DeathSounds) do
-- If the index of the sound is equal to the random number
if index == randomNumber then
-- Create new sound and parent it to the head
local newSound = Instance.new("Sound")
newSound.SoundId = sound.Id
newSound.Parent = character:WaitForChild("Head")
newSound:Play()
-- Wait the set amount of time (task.wait() is faster and more reliable than wait())
task.wait(sound.WaitTime)
newSound:Stop()
newSound.TimePosition = sound.TimePosition
end
end
end)
end)
end)
It is important to note that having a PlayerAdded() listener in a LocalScript will not fire the event for the player running the LocalScript. This is mainly to do with the absolute simultaneous creation of the client-side at the same time PlayerAdded() is also fired. Hence, your code will never run past the PlayerAdded() connection only for yourself. Other connecting clients (other than yourself) should be correctly detected by your LocalScript.
Keeping the above point in mind and a partially re-written code, something along the lines of this should get you the intended result:
-- // Services
local Players = game:GetService("Players")
-- // Variables
local LocalPlayer = Players.LocalPlayer
local DeathSounds = {
{Id = "0000000000", WaitTime = 0.29, TimePosition = 1.34},
{Id = "1111111111", WaitTime = 0.27, TimePosition = 2.64},
{Id = "2222222222", WaitTime = 0.25, TimePosition = 4.55},
}
-- // Functions
local function onDeath(plr)
if plr then
local char = plr.Character
if char then
local humanoid = char:WaitForChild("Humanoid")
math.randomseed(math.random(-os.time(), os.time()))
local randomSound = DeathSounds[math.random(1, #DeathSounds)]
local newSound = Instance.new("Sound")
newSound.SoundId = "rbxassetid://"..randomSound.Id
newSound.Name = "DeathSound"
newSound.Parent = char:WaitForChild("Head")
if not newSound.isLoaded then
newSound.Loaded:Wait()
end
newSound:Play()
task.wait(randomSound.WaitTime)
newSound:Stop()
newSound.TimePosition = randomSound.TimePosition
end
end
end
LocalPlayer.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
onDeath(LocalPlayer)
end)
end)
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
onDeath(player)
end)
end)
end)
EDIT:
I’m a bit unsure about your WaitTime and TimePosition hardcoding. What exactly are you trying to achieve with waiting a specific amount of time? Is the WaitTime different from the sound’s actual length? If they’re the same, you can simply task.wait(newSound.TimeLength).
Secondarily, your TimePosition also seems redundant due to the fact that it will affect nothing about the sound or the way its played. You’re setting it’s position after it has stopped playing and the player then respawns, resulting in all Sound descendants to the character’s Head to be destroyed, which means your setting of TimePosition causes redundancy.
What are you intending for it to do here?
I’d like you to elaborate here a bit please. First of all, by player, are you referring to yourself (LocalPlayer) or other clients? Additionally, how are you testing this?
If there are no errors in sight, it can be significantly helpful to do some basic debugging to check how far does the code go until the intended task derails. This can be anything along the lines of print()ing certain logical parameters or strings at places to assure all obtained values and instances exist, and are correctly assigned.
Oops, that was a messup on my part. The sound does appear into the character (sorry!) but the sound still doesn’t do the :Play() thingy, so you can’t hear it. No errors still.
sorry for the confusion again!
Nevermind, I figured it out. It’s because of the newSound.Loaded:Wait() and the Wait() would just last forever. I know you need to load a sound for it to actually play, so I’ll figure that out myself. So yeahhhhhh. (@ZEDDxR)
The marked solution is quite irrelevant to the original topic. However, I believe they were referring to my added sanity check for the sound’s Loaded state, which is known to cause issues.