Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

FocusLost fires to server incorrectly. Server script prints 3 times instead of once!?

Here is the localscript. This is for a chat inputbar that mimics ROBLOX's. However, remoteevents is not working!

It sends the inputbar's texts to server for processing but the server script prints it THREE times instead of ONCE!

Localscript:

local StarterGui = game:GetService("StarterGui");
local InputGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Chat");
local InputFrame = InputGui.InputFrame;
local Input = InputFrame.Input;
local modules = game.Workspace["Module(s)"];
local chathandler = require(modules.ChatHandler);
local charLimit = 78;
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

local chatAdd = game.Workspace["Remote(s)"].chatAdd;

Input.FocusLost:connect(function(enterWasPressed) --//If enter key was pressed, thus submitting a chatmsg.
	if enterWasPressed then
		print("Executing enterWasPressed...");
		if InputFrame.Visible == true then
			chatAdd:FireServer(Input.Text); --//Problem occurs HERE!
			Input.Text = ("");
			InputFrame.Size = UDim2.new(0.22,0,0.038,0);
		end
	end
end)

Server script:

--//Varuabkes:
local remotes = game.Workspace["Remote(s)"];
local chatMessages = serverData.chatMessages;
local chatAdd = remotes.chatAdd;
--//

chatAdd.OnServerEvent:connect(function(player,msg)
	print("Executing server event...");
	print(msg);
end)

Should be easy as one-two-three. Why isn't it working!? Help.

Image of the chat inputbar:

Click for image.

Output:

**Local output:**
Executing enterWasPressed...
**Server output:**
Executing server event...
Unprecedented!
Unprecedented!
nil

1 answer

Using wait() would probably be very useful in this situation, i've had similar problems and it was fixed in a snap with wait().

local StarterGui = game:GetService("StarterGui");
local InputGui = game.Players.LocalPlayer.PlayerGui:WaitForChild("Chat");
local InputFrame = InputGui.InputFrame;
local Input = InputFrame.Input;
local modules = game.Workspace["Module(s)"];
local chathandler = require(modules.ChatHandler);
local charLimit = 78;
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

local chatAdd = game.Workspace["Remote(s)"].chatAdd;

Input.FocusLost:connect(function(enterWasPressed) --//If enter key was pressed, thus submitting a chatmsg.
    if enterWasPressed then
		wait(0.01)
        print("Executing enterWasPressed...");
        if InputFrame.Visible == true then
            chatAdd:FireServer(Input.Text); --//Problem occurs HERE!
            Input.Text = ("");
            InputFrame.Size = UDim2.new(0.22,0,0.038,0);
        end
    end
end)

2 comments

0

Just a side note: you should just use wait() if you plan on having the least delay with the function. No matter what interval you pass to wait, it's not going to go below (approximately) 1/30th of a second.

0

Same problem is occurring. Not fixed. Help?

This discussion is preserved as it was. New answers, comments, and votes are closed.