HowToRoblox

ChatGuiHandler

Dec 25th, 2020 (edited)
3,526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. local gui = script.Parent
  2. local text = gui.TextBackground.Text
  3. local op1 = gui.TextBackground.Option1
  4. local op2 = gui.TextBackground.Option2
  5.  
  6.  
  7. local range = 7
  8.  
  9. local plr = game.Players.LocalPlayer
  10.  
  11.  
  12. local NPCs = {}
  13.  
  14. for i, descendant in pairs(workspace:GetDescendants()) do
  15.  
  16.     if descendant:FindFirstChild("Interactive") then
  17.         table.insert(NPCs, descendant)
  18.         descendant.Head.InteractGui.Interact.Size = UDim2.new(0, 0, 0, 0)
  19.     end
  20. end
  21.  
  22.  
  23. local closestNPC = script:WaitForChild("nil")
  24. local previousClosestNPC = closestNPC
  25.  
  26. game:GetService("RunService").RenderStepped:Connect(function()
  27.  
  28.  
  29.     previousClosestNPC = closestNPC
  30.     closestNPC = script:WaitForChild("nil")
  31.  
  32.  
  33.     local NPCsInRange = {}
  34.     for i, NPC in pairs(NPCs) do
  35.  
  36.         local distance = plr:DistanceFromCharacter(NPC.HumanoidRootPart.Position)
  37.  
  38.         if distance <= range then
  39.             table.insert(NPCsInRange, NPC)
  40.         end
  41.     end
  42.  
  43.  
  44.     for i, NPCInRange in pairs(NPCsInRange) do
  45.  
  46.         if not closestNPC then closestNPC = NPCInRange end
  47.  
  48.         if closestNPC.Name == "nil" or plr:DistanceFromCharacter(closestNPC.HumanoidRootPart.Position) > plr:DistanceFromCharacter(NPCInRange.HumanoidRootPart.Position) then
  49.             closestNPC = NPCInRange
  50.         end
  51.     end
  52.  
  53.  
  54.     script:WaitForChild("ClosestNPC").Value = closestNPC
  55. end)
  56.  
  57.  
  58. script:WaitForChild("ClosestNPC"):GetPropertyChangedSignal("Value"):Connect(function()
  59.  
  60.     if closestNPC.Name == "nil" then
  61.  
  62.         for i, NPC in pairs(NPCs) do
  63.  
  64.             NPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.3, true)
  65.         end
  66.        
  67.         gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.3, true)
  68.  
  69.     else
  70.  
  71.         if previousClosestNPC and previousClosestNPC.Name ~= "nil" then previousClosestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.3, true) end
  72.  
  73.         closestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(1, 0, 1, 0), "InOut", "Quint", 0.3, true)
  74.  
  75.  
  76.         previousClosestNPC = closestNPC
  77.     end
  78. end)
  79.  
  80.  
  81. local function handleChoice(interactVal, option)
  82.    
  83.     local character = plr.Character or plr.CharacterAdded:Wait()
  84.     local hum = character:WaitForChild("Humanoid")
  85.    
  86.    
  87.     if interactVal == "Would you like extra speed?" then
  88.  
  89.         if option == 1 then hum.WalkSpeed = hum.WalkSpeed + 10 end
  90.  
  91.  
  92.     elseif interactVal == "Would you like extra jump?" then
  93.  
  94.         if option == 1 then hum.JumpPower = hum.JumpPower + 10 end
  95.     end
  96.    
  97.     gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.3, true)
  98. end
  99.  
  100.  
  101. local uis = game:GetService("UserInputService")
  102.  
  103. uis.InputBegan:Connect(function(inp, processed)
  104.  
  105.     if processed or not closestNPC then return end
  106.  
  107.     if inp.KeyCode == Enum.KeyCode.E then
  108.  
  109.         local interactVal = closestNPC.Interactive.Value
  110.        
  111.         text.Text = ""
  112.         gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 0.866, 0), "InOut", "Quint", 0.3, true)
  113.         wait(0.3)
  114.        
  115.        
  116.         op1.MouseButton1Click:Connect(function()
  117.             handleChoice(interactVal, 1)
  118.         end)
  119.         op2.MouseButton1Click:Connect(function()
  120.             handleChoice(interactVal, 2)
  121.         end)
  122.        
  123.        
  124.         for i = 1, #interactVal do
  125.            
  126.             text.Text = string.sub(interactVal, 1, i)
  127.             wait()
  128.         end
  129.     end
  130. end)
Add Comment
Please, Sign In to add comment