script.Parent.Touched:Connect(function(hit)
if played == false then
workspace.Sounds.Error404:Play()
played = true
wait (2) --This Is delay after first play, you can change it if you want to
played = true
end
end)
I already have the code of music playing when a player touches it but after the player dies it still plays, I want it to stop after a player dies but still I cant do it.
What I’d do, is put the LocalScript inside StarterPlayerScripts, and define the Sound outside the Touched event:
local Sound = workspace.Sounds.Error404
local Part = workspace.Part
local HitOnce = false
Part.Touched:Connect(function()
if HitOnce == false then
HitOnce = true
Sound:Play()
wait(5)
HitOnce = false
Sound:Stop()
end
end)
the thing is i want the audio to play when they click a specific thing. like megalonovia when they click normal sans and impotent king when they click king multiverse. i cant have them play megalonovia on king multiverse.
Put a localscript in StarterCharacterScripts to get the player’s character and use the Died event on the humanoid
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
char:WaitForChild("Humanoid").Died:Connect(function()
workspace.Sounds.Error404:Stop()
end)
I guess you want it to stop for the player. So here’s what i’d do. So, as per your script, when the player hits the block it would start playing. So what we want is that (please correct me if im wrong in some assumption
Once the player touches it and the music starts, we dont want to trigger that even until the player dies and does it again.
When the player dies you want it to stop.
So you could do it in different ways. One of them is listening to the .Died event of the players humanoid.
local pl= game.Players.LocalPlayer
local obj= --part you use (I highly recomend you use :WaitForChild()
local sound= workspace:WaitForChild("Sounds"):WaitForChild("Error404")
local canplay=true
obj.Touched:Connect(function(hit)
if hit:IsDescendantOf(pl.Character) and canplay then
canplay=false --We stop it from playing again (for the player) until the player respawns
sound:Play()
local con
con=pl.Character:WaitForChild("Humanoid").Died:Connect(function()
con:Disconnect()
canplay=true --Now player will be able to play it again
sound:Stop()
end)
end
end)
^^ put this in a localscript in StarterPlayer.StarterPlayerScripts. I’ve not tested it but it should work.
You’ll probably need to handle the music changes from the local standpoint, inside a LocalScript inside StarterPlayerScripts defining both the Player & Character together
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Part = workspace.TouchablePart
local ConditionalCheck = false
local Sound = workspace.Sounds.Error404
Part.Touched:Connect(function(Hit)
local HitPlayer = game.Players:GetPlayerFromCharacter(Hit.Parent)
if HitPlayer and ConditionalCheck == false then
ConditionalCheck = true
Sound:Play()
end
end)
Humanoid.Died:Connect(function()
if ConditionalCheck == true then
ConditionalCheck = false
Sound:Stop()
end
end)
Put this LocalScript inside StarterCharacterScripts.
local Players = game:GetService('Players')
local sound = -- sound object
local character = Players.LocalPlayer.Character
local humanoid = character:WaitForChild('Humanoid')
humanoid.Died:Connect(function()
sound:Stop()
end)
Wait, can’t you just put the Sound inside a part & always play it that way when they get teleported? You’d need to change the MaxActivation/MinActivationDistance properties however