So I made a script and it works to a certain level basically meaning it just plays the music but i want it so that when the door part (basically a part) is touched the first time it will play the sound for just that player and when the go through the door again it stops playing the music.
Below is the script
local sound = Instance.new("Sound", workspace) sound.Pitch = 1 -- Speed of the song (Preffer not to change it) sound.SoundId = "rbxassetid://4772956740" -- Put the last numerbs of the link of the song(ROBLOX) that you want sound.Volume = .5 -- Volume(How much power) of the song sound.Looped = true -- If you want the song to repeat after the song is done local part = script.Parent local number = 0 local function onPartTouched(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid and number == 0 then sound:Play() number = number + 1 end end if number == 2 then sound:stop() number = 0 end part.Touched:Connect(onPartTouched)
Your if
statement checking if the number is 2 is outside of the .Touched
statement, so it will not check if it is 2 on .Touched
. To fix this, put it inside of the .Touched
event.
local function onPartTouched(otherPart) local partParent = otherPart.Parent local humanoid = partParent:FindFirstChildWhichIsA("Humanoid") if humanoid and number == 0 then sound:Play() number = number + 1 end if number == 2 then sound:stop() number = 0 end end
You may also want to implement a debounce so the sound does not play a ton of times. Look on the roblox wiki for that.