Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --| SERVICES:
- local PlayerS = game:GetService("Players")
- local plr = PlayerS.LocalPlayer
- local Cha;
- local Hum;
- local RootPart;
- local TweenS = game:GetService('TweenService')
- local TweenInfo_New = TweenInfo.new
- --| VARIABLES:
- local Musics = script.Musics
- Musics.Parent = game.SoundService
- local cache = {}
- local currentMusic;
- --| FUNCTIONS:
- -- Character Handling:
- local HumDied_Connection, Humanoid_Died;
- function Humanoid_Died()
- HumDied_Connection:Disconnect(); HumDied_Connection = nil
- if currentMusic then
- currentMusic()
- end
- end
- local function Character_Added(Character)
- if (not cache[Character]) then
- cache[Character] = tick()
- Cha = Character; Hum = Cha:WaitForChild('Humanoid'); RootPart = Hum.RootPart
- HumDied_Connection = Hum.Died:Connect(Humanoid_Died)
- end
- end
- local function Character_Removing(Character)
- Cha:Destroy(); Hum:Destroy();
- Cha, Hum = nil
- cache[Character] = nil
- end
- local function isPointInPart_Func(Point, Part)
- local relPos = Part.CFrame:PointToObjectSpace(Point)
- return math.abs(relPos.X) < Part.Size.X*.5 and math.abs(relPos.Y) < Part.Size.Y*.5 and math.abs(relPos.Z) < Part.Size.Z*.5
- end
- local function isAlive(humanoid)
- return humanoid and humanoid.Health > 0
- end
- --| SCRIPTS:
- plr.CharacterAdded:Connect(Character_Added); Character_Added(plr.Character or plr.CharacterAdded:Wait());
- plr.CharacterRemoving:Connect(Character_Removing);
- -- Area Music:
- for _,v in ipairs(Musics:GetChildren()) do
- local IsInPart
- local part = v.Part
- local touched_Fn, touchEnded_Fn
- local touched_Ev, touchEnded_Ev = v.Part.Touched, v.Part.TouchEnded
- local touched_Connection, touchEnded_Connection
- local playTween = TweenS:Create(v, TweenInfo_New(1,0,0), {Volume = 5,})
- local stopTween = TweenS:Create(v, TweenInfo_New(2,0,1), {Playing = false, Volume = 0,})
- local diedTween = TweenS:Create(v, TweenInfo_New(2,0,1), {PlaybackSpeed = 0, Volume = .1,})
- local function died_Fn()
- diedTween:Play()
- IsInPart = nil
- touchEnded_Connection:Disconnect()
- touched_Connection = touched_Ev:Connect(touched_Fn)
- end
- -- Play
- function touched_Fn(touchedPart)
- if isAlive(Hum) and (not IsInPart) and touchedPart == RootPart then
- IsInPart = true
- touched_Connection:Disconnect()
- touchEnded_Connection = touchEnded_Ev:Connect(touchEnded_Fn)
- v.PlaybackSpeed = 1
- v:Play()
- stopTween:Cancel()
- playTween:Play()
- currentMusic = died_Fn
- end
- end
- -- Stop
- function touchEnded_Fn(touchedPart)
- if IsInPart and touchedPart == RootPart then
- if (not isPointInPart_Func(RootPart.Position,part)) then
- IsInPart = nil
- touchEnded_Connection:Disconnect()
- touched_Connection = touched_Ev:Connect(touched_Fn)
- playTween:Cancel()
- stopTween:Play()
- currentMusic = nil
- end
- end
- end
- touched_Connection = touched_Ev:Connect(touched_Fn)
- part.Parent = workspace
- end
- return nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement