Created
July 30, 2021 17:06
-
-
Save RobertCodez/abcfab64514685696550477449305f1a to your computer and use it in GitHub Desktop.
NPC Chat Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local gui = script.Parent | |
local text = gui.TextBackground.Text | |
local op1 = gui.TextBackground.Option1 | |
local op2 = gui.TextBackground.Option2 | |
local distanceToUI = 7 | |
local playerd = game.Players.LocalPlayer | |
local NPCs = {} | |
for i, descendant in pairs(workspace:GetDescendants()) do | |
if descendant:FindFirstChild("Interactive") then | |
table.insert(NPCs, descendant) | |
descendant.Head.InteractGui.Interact.Size = UDim2.new(0, 0, 0, 0) | |
end | |
end | |
local closestNPC = script:WaitForChild("Activator") | |
local previousClosestNPC = closestNPC | |
game:GetService("RunService").RenderStepped:Connect(function() | |
previousClosestNPC = closestNPC | |
closestNPC = script:WaitForChild("Activator") | |
local NPCsInRange = {} | |
for i, NPC in pairs(NPCs) do | |
local distance = playerd:DistanceFromCharacter(NPC.HumanoidRootPart.Position) | |
if distance <= distanceToUI then | |
table.insert(NPCsInRange, NPC) | |
end | |
end | |
for i, NPCInRange in pairs(NPCsInRange) do | |
if not closestNPC then closestNPC = NPCInRange end | |
if closestNPC.Name == "Activator" or playerd:DistanceFromCharacter(closestNPC.HumanoidRootPart.Position) > playerd:DistanceFromCharacter(NPCInRange.HumanoidRootPart.Position) then | |
closestNPC = NPCInRange | |
end | |
end | |
script:WaitForChild("ClosestNPC").Value = closestNPC | |
end) | |
script:WaitForChild("ClosestNPC"):GetPropertyChangedSignal("Value"):Connect(function() | |
if closestNPC.Name == "Activator" then | |
for i, NPC in pairs(NPCs) do | |
NPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.4, true) | |
end | |
gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.4, true) | |
else | |
if previousClosestNPC and previousClosestNPC.Name ~= "Activator" then previousClosestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(0, 0, 0, 0), "InOut", "Quint", 0.4, true) end | |
closestNPC.Head.InteractGui.Interact:TweenSize(UDim2.new(1, 0, 1, 0), "InOut", "Quint", 0.4, true) | |
previousClosestNPC = closestNPC | |
end | |
end) | |
local function handleChoice(interactVal, option) | |
local character = playerd.Character or playerd.CharacterAdded:Wait() | |
local hum = character:WaitForChild("Humanoid") | |
if interactVal == "Would you like to open the Shop?" then | |
if option == 1 then | |
print("Clicked") | |
game.ReplicatedStorage.FireEvent:FireServer() | |
print("fired") | |
end | |
elseif interactVal == "hi" then | |
if option == 1 then print("hello") end | |
end | |
gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 1 + gui.TextBackground.Size.Y.Scale, 0), "InOut", "Quint", 0.4, true) | |
end | |
local UIP = game:GetService("UserInputService") | |
UIP.InputBegan:Connect(function(inp, processed) | |
if processed or not closestNPC then return end | |
if inp.KeyCode == Enum.KeyCode.T then | |
local theQ = closestNPC.Interactive.Value | |
text.Text = "" | |
gui.TextBackground:TweenPosition(UDim2.new(0.5, 0, 0.866, 0), "InOut", "Quint", 0.4, true) | |
wait(0.3) | |
op1.MouseButton1Click:Connect(function() | |
handleChoice(theQ, 1) | |
end) | |
op2.MouseButton1Click:Connect(function() | |
handleChoice(theQ, 2) | |
end) | |
for i = 1, #theQ do | |
text.Text = string.sub(theQ, 1, i) | |
wait() | |
end | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment