To create this functionality, you'll need to set up your NPC, GUI, and scripting environment correctly. Below is a step-by-step guide along with the necessary script.
1. Setting Up the NPC and ProximityPrompt:
2. Creating the GUI:
3. Writing the LocalScript:
- Insert a LocalScript into
StarterPlayerScripts:
- This script will handle displaying the GUI and the typing effect when the ProximityPrompt is activated.
Here's the script you need to insert into the LocalScript:
-- LocalScript in StarterPlayerScripts
local player = game.Players.LocalPlayer
local npc = workspace:WaitForChild("Level 1")
local proximityPrompt = npc:WaitForChild("UpperTorso"):WaitForChild("ProximityPrompt")
local playerGui = player:WaitForChild("PlayerGui")
local npcDialogueGui = playerGui:WaitForChild("NPCDialogue")
-- Ensure the GUI is hidden at the start
npcDialogueGui.Enabled = false
local debounce = false -- Prevents multiple activations
-- Function to create the typing effect
local function typeText(textLabel, textToType, typingSpeed)
textLabel.Text = ""
for i = 1, #textToType do
textLabel.Text = string.sub(textToType, 1, i)
wait(typingSpeed)
end
end
-- When the ProximityPrompt is triggered
proximityPrompt.Triggered:Connect(function()
if debounce then return end
debounce = true
-- Make the GUI visible
npcDialogueGui.Enabled = true
-- Get the TextLabel
local textLabel = npcDialogueGui.Frame:WaitForChild("Text")
local typingSpeed = 0.05 -- Adjust typing speed as needed
-- Start the typing effect
typeText(textLabel, "You think you're smart enough to defeat me?!", typingSpeed)
-- Get the Accept and Decline buttons
local acceptButton = npcDialogueGui.Frame:WaitForChild("Accept")
local declineButton = npcDialogueGui.Frame:WaitForChild("Decline")
-- Connect the Accept button
acceptButton.Activated:Connect(function()
-- Actions when Accept is clicked
print("Player accepted the challenge!")
-- Hide the GUI and reset
npcDialogueGui.Enabled = false
textLabel.Text = ""
debounce = false
-- Add any additional functionality here (e.g., start a battle)
end)
-- Connect the Decline button
declineButton.Activated:Connect(function()
-- Actions when Decline is clicked
print("Player declined the challenge.")
-- Hide the GUI and reset
npcDialogueGui.Enabled = false
textLabel.Text = ""
debounce = false
-- Add any additional functionality here
end)
end)
4. Explanation of the Script:
-
Variables:
player: References the local player.
npc: Finds the NPC named "Level 1" in the workspace.
proximityPrompt: Gets the ProximityPrompt inside the NPC's UpperTorso.
playerGui: References the player's GUI container.
npcDialogueGui: References the "NPCDialogue" GUI.
-
Initial Setup:
- Sets the
Enabled property of npcDialogueGui to false to ensure it's hidden initially.
- A
debounce variable is used to prevent the prompt from being activated multiple times simultaneously.
-
typeText Function:
- A function that simulates typing by updating the
Text property of the label one character at a time.
typingSpeed controls how fast the characters appear.
-
Triggered Event:
- When the ProximityPrompt is triggered, it checks the debounce.
- Displays the GUI and starts the typing effect.
- Waits for the player to click either "Accept" or "Decline".
-
Button Events:
- Accept Button:
- Prints a message to the output (you can replace this with actual game logic, like starting a battle).
- Hides the GUI and resets variables.
- Decline Button:
- Similarly handles when the player declines.
- Hides the GUI and resets variables.
5. Testing:
- Playtest your game:
- Ensure that when you approach "Level 1" and activate the ProximityPrompt, the GUI appears.
- The dialogue should display with a typing effect.
- The "Accept" and "Decline" buttons should respond when clicked, and the GUI should close appropriately.
6. Additional Notes:
-
Adjust Typing Speed:
- You can change the
typingSpeed variable to make the typing effect faster or slower.
-
Custom Functionality:
- Replace the
print statements in the button functions with your own game logic, such as initiating a combat sequence or changing the game state.
-
GUI Design:
- Feel free to customize the appearance of the GUI elements to match your game's style.
This script and setup will provide the functionality you're looking for: interacting with an NPC via a ProximityPrompt, displaying dialogue with a typing effect, and offering "Accept" and "Decline" options to the player.