Advertisement
The_Keeminator

Npc Roblox

Apr 13th, 2020
5,752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.16 KB | None | 0 0
  1. local system = {}
  2. local assets = game.ReplicatedStorage.Assets
  3. local animation = require(game.ReplicatedStorage.Modules.animation)
  4. local text = require(game.ReplicatedStorage.Modules.text)
  5. local uis = game:GetService("UserInputService")
  6.  
  7. --Settings
  8. local settings = {
  9.     distanceOffset = 55,
  10.     animationOffset = 25,
  11.     animationCooldownTime = 25,
  12.     key = "e",
  13. }
  14.  
  15. --Functions
  16. function system.new(npctype,spawna,messages,cameraangle)
  17.     local player = game.Players.LocalPlayer
  18.     local char = player.Character or player.CharacterAdded:wait()
  19.     local camera = workspace.CurrentCamera
  20.     local gui = player:WaitForChild("PlayerGui")
  21.     local cooldown = false
  22.    
  23.     --Spawn npc
  24.     local npc = assets[npctype]:Clone()
  25.     local cIn
  26.     pcall(function()
  27.         cIn=spawna["ClassName"]
  28.     end)
  29.     if cIn then
  30.         --If part or model.
  31.         if spawna:IsA("BasePart") then
  32.             npc:SetPrimaryPartCFrame(spawna.CFrame+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
  33.         elseif spawna:IsA("Model") and spawna.PrimaryPart then
  34.             npc:SetPrimaryPartCFrame(spawna.PrimaryPart.CFrame+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
  35.         else
  36.             warn("Failed to recieve cframe of object: ".. spawna.Name)
  37.         end
  38.     elseif spawna.p then
  39.         npc:SetPrimaryPartCFrame(spawna+Vector3.new(0,npc.HumanoidRootPart.Size.Y+0.25,0))
  40.     else
  41.         warn("No object of CFrame given.")
  42.     end
  43.     npc.Parent = workspace.npc
  44.    
  45.     --Setup the gui
  46.     local _gui = gui:WaitForChild("MainUI")
  47.    
  48.     _gui.alert.center.letter.Text = string.upper(settings.key)
  49.     _gui.message.letter.Text = string.upper(settings.key)
  50.     _gui.message.center.title.Text = ""
  51.    
  52.     --Play idle animation
  53.     local itrack = animation:animate(507766388,npc.Humanoid)
  54.    
  55.     --Check for the NPC's Line of sight.
  56.     local raus,vaus
  57.     local render = game:GetService("RunService").RenderStepped:connect(function(dt)
  58.         local dist = (npc.HumanoidRootPart.CFrame.p-char.HumanoidRootPart.CFrame.p).magnitude
  59.         if not cooldown and not vaus then
  60.             if dist <= settings.animationOffset then
  61.                 local ray = Ray.new(npc.HumanoidRootPart.CFrame.p,npc.HumanoidRootPart.CFrame.lookVector*500)
  62.                 local object,surfaceposition = workspace:FindPartOnRay(ray,npc)
  63.                 if object then
  64.                     local target = object.Parent
  65.                     if target:FindFirstChild("Humanoid") and game.Players:FindFirstChild(target.Name) then
  66.                         cooldown=true
  67.                         local track = animation:animate(507770239,npc.Humanoid)
  68.                         wait(1.5)
  69.                         track:stop()
  70.                         spawn(function()
  71.                             wait(settings.animationCooldownTime)
  72.                             cooldown=false
  73.                         end)
  74.                     end
  75.                 end
  76.             end
  77.         end
  78.         if not vaus then
  79.             if dist <= settings.distanceOffset then
  80.                 --Raise the gui
  81.                 _gui.alert:TweenPosition(UDim2.new(0,0,0,9,0),"Out","Linear",0.3,false)
  82.                 raus=true
  83.             else
  84.                 _gui.alert:TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,false)
  85.                 raus=false
  86.             end
  87.         end
  88.     end)
  89.    
  90.     --Detect user input, allow for conversation.
  91.     local input = uis.InputBegan:connect(function(key,gpe)
  92.         if key.KeyCode == Enum.KeyCode[string.upper(settings.key)] and vaus ~= true then
  93.             vaus=true
  94.             raus=false
  95.             _gui.alert:TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,true)
  96.            
  97.             --Rotate player accordingly to the NPC
  98.             char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame.p.npc.HumanoidRootPart.CFrame.p)
  99.            
  100.             --Set camera position
  101.             camera.CameraType = Enum.CameraType.Scriptable
  102.             pcall(function()
  103.                 cIn=cameraangle["ClassName"]
  104.             end)
  105.             if cIn then
  106.                 --If part or model.
  107.                 if cameraangle:IsA("BasePart") then
  108.                     camera.CFrame = cameraangle.CFrame
  109.                 elseif cameraangle:IsA("Model") and cameraangle.PrimaryPart then
  110.                     camera.CFrame = cameraangle.PrimaryPart.CFrame
  111.                 else
  112.                     warn("Failed to recieve cframe of object: " .. cameraangle.Name)
  113.                 end
  114.             elseif cameraangle.p then
  115.                 camera.CFrame = cameraangle
  116.             else
  117.                 warn("No object of CFrame given.")
  118.             end
  119.            
  120.             --Stop player motion
  121.             local ws,jp = char.Humanoid.Walkspeed,char.Humanoid.JumpPower
  122.             char.Humanoid.WalkSpeed = 0
  123.             char.Humanoid.JumpPower = 0
  124.             _gui.message.TweenPosition(UDim2.new(0,0,0,9,0),"Out","Linear",0.3,true)
  125.             wait(0.3)
  126.             for a,b in pairs(messages) do
  127.                 text.gradual(b,_gui.message.center.title)
  128.                 for c=1,0,-0.1 do
  129.                     _gui.message.letter.BackroundTransparency = c
  130.                     _gui.message.letter.TextTransparency = c
  131.                     wait()
  132.                 end
  133.                 local subimput,success
  134.                 subimput = uis.ImputBegan:connect(function(key,gpe)
  135.                     if key.KeyCode == Enum.KeyCode[string.upper(settings.key)] then
  136.                         subimput:disconnect()
  137.                         success=true
  138.                     end
  139.                 end)
  140.                 repeat wait() until success
  141.                 spawn(function()
  142.                     for c=0,1,0.1 do
  143.                         _gui.message.letter.BackroundTransparency = c
  144.                         _gui.message.letter.TextTransparency = c
  145.                         wait()
  146.                     end
  147.                 end)
  148.                 if a == #messages then
  149.                     _gui.message.TweenPosition(UDim2.new(0,0,1.2,0),"In","Linear",0.3,true)
  150.                     char.Humanoid.WalkSpeed = ws
  151.                     char.Humanoid.JumpPower = jp
  152.                     camera.CameraType = Enum.CameraType.Custom
  153.                     vaus=false
  154.                     _gui.message.enter.title.Text = ""
  155.                 end
  156.             end
  157.         end
  158.     end)
  159.    
  160.     return function()
  161.         itrack:Stop()
  162.         input:disconnect()
  163.         render:disconnect()
  164.         npc:Destroy()
  165.     end
  166. end
  167.  
  168. return system
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement