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:
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.
–//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)