This is quite simple but i have a script where when touching a certain part, a sound plays. What would be the script to stop the song completely after i die?
You can use Humanoid.Died
events and then stop the music afterwards.
I have made this script
player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
Sound:Stop()
What is the problem with this script?
Try do:
game.Players.PlayerAdded:Connect(function(player)
--play music
local char = player.Character
if char then
char.Humanoid.Died:Connect(function()
--stop music
end
end)
end)
It doesn’t seem to work.
This is the sound play script i have:
local sound = Instance.new(“Sound”, Workspace)
sound.Pitch = 1
sound.SoundId = “rbxassetid://144405051”
sound.Volume = .5
sound.Looped = true
script.Parent.Touched:connect(function()
sound:play()
end)
Can you screenshot your script with my code included so i can see what you have done?
You didnt call the sound first, so instead of the comments, do your Instance.new sound code, and capitalize Play() and Stop()
Where would i put the>local sound = Instance.new(“Sound”, Workspace) code?
Like i said, put it in the first comments inside the player added event. sound
is nil as it hasnt been defined, and adding the instance will define it.
You need to define a sound Instance if you have one in the workspace link it to the variable sound
.
If not create a new Instance put the sound ID and play it.
Your code should look like this. (This must be a local script and placed in StarterPlayerScripts.)
local Player = game.Players.LocalPlayer
local Sound = Instance.new("Sound",workspace)
local Id = 0000000000
Sound.SoundID = Id
Sound:Play()
Player.Character:WaitForChild("Humanoid").Died:Connect(function()
Sound:Stop()
end)
I have this code currently in workspace (note that the song plays only when i touch a specific part)
local sound = Instance.new(“Sound”,workspace)
sound.Pitch = 1
sound.SoundId = “rbxassetid://144405051”
sound.Volume = .5
sound.Looped = true
script.Parent.Touched:connect(function()
sound:Play()
end)
and this script that you gave me inside starterplayerscripts
local Player = game.Players.LocalPlayer
local Sound = Instance.new(“Sound”,workspace)
local Id = 0000000000
Sound.SoundID = 144405051
Sound:Play()
Player.Character:WaitForChild(“Humanoid”).Died:Connect(function()
Sound:Stop()
end)
Can you tell me what i did wrong?
workspace.YourPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then --check if player
YourSound:Play()
local Humanoid = hit.Parent:WaitForChild("Humanoid") --Lets wait for the humanoid since that might be the issue
Humanoid.Died:Connect(function()
YourSound:Stop()
end)
end
end)
Make sure that its not a local script otherwise PlayerAdded wont work and change YourSound and YourPart to the name of your sound. To prevent multiple runs then add a debounce.
Ah yeah it’s all good now thanks