Typewrite text on hover over part

I want to make it where when I hover my mouse over a specific part, the text typewrites next to the mouse. I managed to create a script where the text appears when your mouse is over the part normally, but I can’t manage to integrate typewriting into it.

As for the issue, I’m unsure of what I’m doing wrong, due to there being no visible or apparent errors. I’m just assuming that it’s because of line 17-18:


(All files are placed in the correct spot)

I have tried messing around with it for a bit.

1 Like

wait() wont work in a RenderStepped loop, Change the loop into a while true do

I fixed this just now, but there might be something else causing it as well, as it still isn’t appearing.

The problem is that every frame the mouse is hovering over the targeted part, it is trying to write the text in TextGui. This means that every frame, the TextGui is writing over itself as every frame is trying to write the same thing.

Tried changing but I may have done it wrong, since It’s still not showing up.
If there’s a mistake I made, what part is it?

1 Like

Can you paste your code? I think I know a fix but I would rather copy and paste it rather than write it all myself and maybe get it wrong.

You should be using PlayerGui not ScreenGui, for example:

TextLabel = Player.PlayerGui.ScreenGui.TextLabel

Which is probably why you see no change.

And also, I highly recommend you use utf8.graphemes when using string.sub

–//Client
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

–//GUI
local TextGUI = script.Parent.ScreenGui.TextLabel
local text = “sample text”

–//Main
while true do
task.wait()
local X = Mouse.X
local Y = Mouse.Y
if Mouse.Target then
if Mouse.Target.Name == “torch1Part” then
TextGUI.Position = UDim2.new(0,X,0,Y)
for i = 1, #text do
TextGUI.Text = string.sub(text, 1, i)
task.wait(0.0875)
end
else
TextGUI.Visible = false
end
end
end

The way you’re retrieving the mouse’s position data is probably why you can’t see it.

Fixed code:

--//Services
local Players = game:GetService("Players")

--//Variables
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local TextGUI = script.Parent.ScreenGui.TextLabel

--//Controls
local Text = "sample text"

--//Loops
while task.wait() do	
	if not Mouse.Target then
		return
	end
	
	if Mouse.Target.Name == "torch1Part" then
		TextGUI.Position = UDim2.new(Mouse.X/Mouse.ViewSizeX, 0, Mouse.Y/Mouse.ViewSizeY, 0)
		
		for i = 1, #Text do
			TextGUI.Text = string.sub(Text, 1, i)
			task.wait(0.0875)
		end
	else
		TextGUI.Visible = false
	end
end

I changed your while do loop and replaced it with Mouse.Move instead. I also added a function that only runs if the correct part is detected.

local Players = game:GetService('Players');

local Player = Players.LocalPlayer;
local Mouse = Player:GetMouse();

local TextGui = script.Parent; -- I put the script as a child of the TextGui

local PartDetected = false; -- A Debounce to keep track of wether the correct part is detected or not

local function displayText(message)
	if not PartDetected then return end;
	
	for i = 1, #message do
		if not PartDetected then TextGui.Text = "" return end; --Breaks the loop if vision is lost
		TextGui.Text = string.sub(message, 1, i);
		wait(0.1)
	end
end

Mouse.Move:Connect(function()
	local X = Mouse.X;
	local Y = Mouse.Y;
	
	TextGui.Position = UDim2.new(0, X, 0, Y);
	
	if Mouse.Target and Mouse.Target.Name == "torch1Part" then
		TextGui.Visible = true;
		if not PartDetected then -- Only runs displayText() the first time the object is detected
			PartDetected = true;
			displayText("Torch");
		end
	else
		PartDetected = false;
		TextGui.Visible = false;
	end
end)

Here is the script in action:
https://gyazo.com/7dcac6463550b8d2d93f0eb89e4d8729

This can also be very easily optimized to support many different objects at once.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.