Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local system = {}
- local assets = game.ReplicatedStorage.Assets
- local animation = require(game.ReplicatedStorage.Modules.animation)
- local text = require(game.ReplicatedStorage.Modules.text)
- local uis = game:GetService("UserInputService")
- --Settings
- local settings = {
- distanceOffset = 55,
- animationOffset = 25,
- animationCooldownTime = 25,
- key = "e",
- }
- --Functions
- function system.new(npctype,spawna,messages,cameraangle)
- local player = game.Players.LocalPlayer
- local char = player.Character or player.CharacterAdded:wait()
- local camera = workspace.CurrentCamera
- local gui = player:WaitForChild("PlayerGui")
- local cooldown = false
- --Spawn npc
- local npc = assets[npctype]:Clone()
- local cIn
- pcall(function()
- cIn=spawna["ClassName"]
- end)
- if cIn then
- --If part or model.
- if spawna:IsA("BasePart") then
- npc:SetPrimaryPartCFrame(spawna.CFrame+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
- elseif spawna:IsA("Model") and spawna.PrimaryPart then
- npc:SetPrimaryPartCFrame(spawna.PrimaryPart.CFrame+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
- else
- warn("Failed to recieve cframe of object: ".. spawna.Name)
- end
- elseif spawna.p then
- npc:SetPrimaryPartCFrame(spawna+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
- else
- warn("No object of CFrame given.")
- end
- npc.Parent = workspace.npc
- --Setup the gui
- local _gui = gui:WaitForChild("MainUI")
- _gui.alert.center.letter.Text = string.upper(settings.key)
- _gui.message.letter.Text = string.upper(settings.key)
- _gui.message.center.title.Text = ""
- --Play idle animation
- local itrack = animation:animate(507766388,npc.Humanoid)
- --Check for the NPC's Line of sight.
- local raus,vaus
- local render = game:GetService("RunService").RenderStepped:connect(function(dt)
- local dist = (npc.HumanoidRootPart.CFrame.p-char.HumanoidRootPart.CFrame.p).magnitude
- if not cooldown and not vaus then
- if dist <= settings.animationOffset then
- local ray = Ray.new(npc.HumanoidRootPart.CFrame.p,npc.HumanoidRootPart.CFrame.lookVector*500)
- local object,surfaceposition = workspace:FindPartOnRay(ray,npc)
- if object then
- local target = object.Parent
- if target:FindFirstChild("Humanoid") and game.Players:FindFirstChild(target.Name) then
- cooldown=true
- local track = animation:animate(507770239,npc.Humanoid)
- wait(1.5)
- track:stop()
- spawn(function()
- wait(settings.animationCooldownTime)
- cooldown=false
- end)
- end
- end
- end
- end
- if not vaus then
- if dist <= settings.distanceOffset then
- --Raise the gui
- _gui.alert:TweenPosition(UDim2.new(0,0,0,9,0),"Out","Linear",0.3,false)
- raus=true
- else
- _gui.alert:TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,false)
- raus=false
- end
- end
- end)
- --Detect user input, allow for conversation.
- local input = uis.InputBegan:connect(function(key,gpe)
- if key.KeyCode == Enum.KeyCode[string.upper(settings.key)] and vaus ~= true then
- vaus=true
- raus=false
- _gui.alert:TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,true)
- --Rotate player accordingly to the NPC
- char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.p.npc.HumanoidRootPart.CFrame.p)
- --Set camera position
- camera.CameraType = Enum.CameraType.Scriptable
- pcall(function()
- cIn=cameraangle["ClassName"]
- end)
- if cIn then
- --If part or model.
- if cameraangle:IsA("BasePart") then
- camera.CFrame = cameraangle.CFrame
- elseif cameraangle:IsA("Model") and cameraangle.PrimaryPart then
- camera.CFrame = cameraangle.PrimaryPart.CFrame
- else
- warn("Failed to recieve cframe of object: " .. cameraangle.Name)
- end
- elseif cameraangle.p then
- camera.CFrame = cameraangle
- else
- warn("No object of CFrame given.")
- end
- --Stop player motion
- local ws,jp = char.Humanoid.Walkspeed,char.Humanoid.JumpPower
- char.Humanoid.WalkSpeed = 0
- char.Humanoid.JumpPower = 0
- _gui.message.TweenPosition(UDim2.new(0,0,0,9,0),"Out","Linear",0.3,true)
- wait(0.3)
- for a,b in pairs(messages) do
- text.gradual(b,_gui.message.center.title)
- for c=1,0,-0.1 do
- _gui.message.letter.BackroundTransparency = c
- _gui.message.letter.TextTransparency = c
- wait()
- end
- local subimput,success
- subimput = uis.ImputBegan:connect(function(key,gpe)
- if key.KeyCode == Enum.KeyCode[string.upper(settings.key)] then
- subimput:disconnect()
- success=true
- end
- end)
- repeat wait() until success
- spawn(function()
- for c=0,1,0.1 do
- _gui.message.letter.BackroundTransparency = c
- _gui.message.letter.TextTransparency = c
- wait()
- end
- end)
- if a == #messages then
- _gui.message.TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,true)
- char.Humanoid.WalkSpeed = ws
- char.Humanoid.JumpPower = jp
- camera.CameraType = Enum.CameraType.Custom
- vaus=false
- _gui.message.enter.title.Text = ""
- end
- end
- end
- end)
- return function()
- itrack:Stop()
- input:disconnect()
- render:disconnect()
- npc:Destroy()
- end
- end
- return system
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement