TypeWrite Problems

So I have an NPC chat system that I attempted to compress into a singular LocalScript which does work but there are a few errors I’m not sure how to fix without having to redo everything


The main problem is in this part of the script where pressing and quickly moving my cursor off of the NPCs hitbox causes the script to skip the part of the code I put in a red bracket. (line 55-68)

image
The second problem might be in the typewrite function? If I press on the NPCs hitbox a bunch of times it’ll try to run every time I clicked on the NPC and tries to overlap the text which causes it to glitch out.

(P.S. I’ll take whatever scripting tips that I can add into my script aswell)

You can use a debounce to fix the double click issue. This makes sure when it’s clicked once it won’t run again until the boolean is changed back. This is just an example:

local deb = true
mouse.Button1Down:Connect(function()
	if deb then
		deb = false
		wait(1)
		typeWriter() -- only runs once because deb is now false
	end
	wait(0.5)
	deb = true
end)

At the start do this, then Target will always be the same through the script even when the mouse it moved away.

local Target = mouse.Target

Thanks for recommending the debouce, worked perfectly : )
Im gonna guess the code for the second option wouldnt work so Im gonna continue waiting for a solution

Sorry if I confused you, I had striked the last part meaning it’s to be ignored. You should only have looked at the middle part. I removed it if it helps not to confuse you.

(Just a heads up, all swear words are banned on Roblox and can result in an account termination. I recommend you remove them from your script ASAP)

You’re trying to get the mouse’s Target after the 1.5 seconds are elapsed. By that time, it is normal that the cursor might have moved away and changed it’s target to another one.
To fix that issue, you should try to, like @MineJulRBX said, immediately find the target before the wait(1.5), and then use it later to deduce which text you need to type.

local target = mouse.Target
--random tweenings
task.wait(1.5) --(thanks to @GummyScript for reminding me) 
--more random tweenings
if target:FindFirstChild("NPCname1") then
--rude words
elseif target:FindFirstChild("NPCname2") then
--kind words
end 

Also, i dont understand why you decided to tween the TextLabel’s properties seperately?
You do know you can tween multiple properties at once right?

1 Like

You should be using task.wait() not wait()

2 Likes

Yeah don’t worry it was a test world that was only saved locally on my computer and not published to Roblox lol, I just wanted something funny to test the script