How to make "E" proximity trigger visuals

i want to make a npc E to chat i know about how to create E to start the chat but what i wanna know is that how do i make it so the E button i created goes to the npcs torso? and when i press E the camera gets near the NPC i tried a 200 line script and it gave me 40 errors ( yep) so i have come to devforum

script ive tried so far"

--// Do not COPY!!!!





--// Services


local RunServices = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")





--// Main Variales


local DetectedNPC = nil
local Detected = false
local Chatting = false
local Skip = false
local Exit = false





--// Player


local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local Gui = script.Parent
local Sounds = Gui.Sounds
local PromptLabel = Gui.PromptLabel
local LineLabel = Gui.LineLabel





--// Character


local Character = Player.Character or Player.CharacterAdded:Wait()
local CharHMR = Character:WaitForChild("HumanoidRootPart")





--// NPC


local NPCS = game.Workspace:WaitForChild("NPCS")





--// Functions


UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.X then
		if Chatting == true then
			Skip = true
			Sounds.Click:Play()
		end
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Z then
		if Chatting == true then
			Skip = true
			Sounds.Click:Play()
		end
	end
end)

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		
		
		if Detected == true then
			local Lines = DetectedNPC:GetChildren()
			
			if Lines then
				Sounds.Click:Play()
				
				Chatting = true
				Detected = false
				
				LineLabel.Text = " "	
				
				PromptLabel:Tweensize(Udim2.new.new(0, 0, 0, 0), "Out", "Linear")
				LineLabel:Tweensize(Udim2.new.new(0, 0, 0.8, 0), "In", "Linear")
				
				wait(0.5)
				
				for i, Line in pairs(Lines:GetChildren())
				do	local Text = Line.Value
					
					for i = 1 ,Text do
						LineLabel.Text = string.sub(Text, 1, i)
						Sounds.Talk:Play()
						if Skip == true then
							Skip = false
							LineLabel.Text = Text
							break
						end
						if Exit == true then
							break
						end	
						wait(0.7)
					end
			end
		end 
		
		
	end
end

RunServices.RenderStepped:Connect(function()
	
	if Detected == false and Chatting == false then
		for i , NPCS in pairs(NPCS:GetChildren()) do
			local Humanoid = NPC:FindFirstChild("Humanoid")
			local HMR = NPC:FindFirstChild("HumanoidRootPart")
			if Humanoid and HMR then
				if (HMR.Position - CharHMR.Position).Magnitiude < 15 then
					Detected = true
					DetectedNPC = NPC
					PromptLabel:TweenSize(Udim2.new.new.new.new()(0,60,0,60), "In" , "Linear" )
					print(DetectedNPC.Name)
				end
			end
			
		end
	end
		
	
end)

if Detected == true and Chatting == false then
	local Humanoid = NPC:FindFirstChild("Humanoid")
	local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
	if Humanoid and HMR then
		if (HMR.Position - CharHMR.Position).Magnitiude > 15 then
			Detected = false
			DetectedNPC = nil
			PromptLabel:TweenSize(Udim2.new(0,0,0,0), "Out" , "Linear" )
			print ("THIS SCRIPT IS HARD BUT NO LONGER DETECTED NPC")
		else
			local WTSP = Camera:WorldToScreenPoint(HMR.Position)
			Promptlabel.Position = Udim2.new.new.new.new(0, WTSP.X , 0 , WTSP.Y)
			
		end
	end
end
if chatting == true then
	local Humanoid = NPC:FindFirstChild("Humanoid")
	local HMR = DetectedNPC:FindFirstChild("HumanoidRootPart")
	
	if Humanoid and HMR then
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = Camera.CFrame:Lerp(HMR.CFrame * CFrame.new(-4,4,-7)* CFrame:FromOrientation(math.rad(-20), math.rad(215), 0),0.07)
	end
else
	Camera.CameraType = Enum.CameraType.Custom
end)
3 Likes

Can you provide your current script? so that the other scripters can tell you if there’s a mistake?

i am not on my main pc right now but i had used a vid i can give half the script

wait this is WRONG
ill send right script

ive included the script in my question now

You’re attempting to create a proximity detection to allow nearby players to interact with, by any key chosen. In another topic, this has been answered(and there’s many more). Use the search function, unless you don’t have any applicable solution from there.

The title is misleading and should be properly renamed. I have not thought of any yet.

2 Likes

i am not using billboard gui i am using a textlael

You will still need to parent the text label to a Billboard GUI to get the behavior you want.

You are not reading the reply properly. Ignore the BillboardGui completely and the visuals. The mechanics is mentioned above.

Title has been renamed properly. Changed twice, because it’s about the visuals.

1 Like
-- script not tested, must be inside startergui ; localscript
local e = false
local u = game:GetService('UserInputService')
u.InputBegan:Connect(function(k)
    if k.KeyCode == Enum.KeyCode.E then
       if e == true then
game.SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
      else
game.SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)
       end
    end
end)

Poorly formatted it would work though-
Nevermind: you changed the title, the topic is different
this is a chat gui trigger

The solution to this is a client-sided visuals management, where the client script would detect the distance and if close enough, the BillboardGui would be visible in place(with or without animation through tweening), with the button. Pressing E on the client changes the button in animation.

None of that is connected to the server. Server handles the necessary code that reads the player inputs(and maybe filter it).

so how would i make it detect distance i want it to detect if its 6 studs away

You will need to get the magnitude between player character and the other part, and then compare that distance. Then you will need to connect UIS for the keycode E, and the rest you probably know.

You can check how magnitude works here:

You would do the constant detection on client, let the server verify its distance upon request/trigger, and then server returning the response to proceed with the dialogue.

I believe you have to use the RunService’s events to do the checking constantly.

Player:DistanceFromCharacter() would be recommended function to check distance between the character and the target part.

what should i use magnitude or player:distancefromcharacter ?

You can use Player:DistanceFromCharacter() , I just didn’t knew that function exists, it internally uses magnitude to check it anyways.

so if i name my gui E LABEL

then code be

 Player:DistanceFromCharacter(Vector3.new ()
e label.visible = true

what i want to know is that how do i make it so the gui gets to the players torso (or humanoid root part)

The function returns a number, which is the distance. You could put it in an if statement.

if Player:DistanceFromCharacter(proximityDetector) < 10 then -- within 10 studs
    print("You are now within radius.")
end

The function is built around the character, most likely the HumanoidRootPart, which exists in almost all characters.

so if its built in humanoid root part how will i still make the gui i made go the player s humanoidrootpart ?