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

RemoteEvent's OnServerEvent sends print message three times on a simple script!?

I'm having a problem with RemoteEvent. It's sending my print message three times when it should really be sending it once. Can you take a look?

This is the hierarchy of what happens: Localscript->Modulescript activates RemoteEvent->OnServerEvent hears that activation and SHOULD fire only one print message yet fires three!?

Localscript (sniped to the part where it sends a fire to the modulescript) :

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
			chathandler.add(Input.Text); --//Modulescript usage kicks in here.
			Input.Text = ("");
			InputFrame.Size = UDim2.new(0.22,0,0.038,0);
			InputFrame.Visible = false;
		end
	end
end)
--[===[Deprecated:
--]===]

Modulescript:

local chathandler = {}
--//Events:
local chatAdd = game.Workspace["Remote(s)"].chatAdd;
--//

function chathandler.add(msg)
	print("Executing chathandler.add...");
	chatAdd:FireServer(msg);
end

return chathandler;

Serverscript w/the OnServerEvent listener:

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

Output:

http://i.imgur.com/AJ0EYgj.png

What the hell is happening? All I have is a simple input bar that mimics the ROBLOX chatbar. I know that they recently released their own chat API but I want to try to make mine so it's unique and it's mine. Help???

It's worth noting that even if I fire to the OnServerEvent listener (RemoteEvent) directly within the local script, the same result happens.

Update(s):

*I did a quick fix and removed the Input:ReleaseFocus() line and this is now the output:

Executing enterWasPressed...
Executing chathandler.add...
Hi, testing!
nil
Executing server event...
Hi, testing!

It didn't change much! Somebody help please. Here's the full localscript:

--> Initial frame size on y's: 0.038 - > to: 0.06
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)

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
			chathandler.add(Input.Text); --//Modulescript usage kicks in here.
			Input.Text = ("");
			InputFrame.Size = UDim2.new(0.22,0,0.038,0);
			InputFrame.Visible = false;
		end
	end
end)

Input.Changed:connect(function(whatChanged) --//Input monitor, monitors keystrokes while user is inputting text.
	if whatChanged == ("Text") then
	if string.len(Input.Text) >= charLimit then
		Input.Text = Input.Text:sub(1,charLimit);
		if InputFrame.ErrorC.Visible == false then
			InputFrame.ErrorC.Visible = true;
		end
		else
			if InputFrame.ErrorC.Visible == true then
				InputFrame.ErrorC.Visible = false;
			end
	end
	if Input.TextFits == false then
	if InputFrame.Size == UDim2.new(0.22,0,0.038,0) then
		InputFrame.Size = UDim2.new(0.22,0,0.06,0);
	end
	end
	end
		
end)

function onKeyPress(actionName, userInputState, inputObject) --//When you press the "/" key, mimicking ROBLOX chat.
	if userInputState == Enum.UserInputState.Begin and actionName == "keyPress/" then
		if InputFrame.Visible == false then
		InputFrame.Visible = true;
		Input:CaptureFocus();
		end
	end
end

game.ContextActionService:BindAction("keyPress/", onKeyPress, false, "/"); --//Binds.

--[===[Deprecated:
--]===]

Help!

Note: This is just a user input bar that mimics ROBLOX's chat input bar so when the user presses "/" they enter text and when focus is lost it fires to modulescript...

4 comments

0

Input:ReleaseFocus(). What's the point of this line? Focus is released automatically when enter is pressed.

0

I did a quick fix, can you check now? Still pretty much same results!

0

That output looks right to me. Are you testing with play solo? Have you tried this with Start Server and looked at the output on the server versus the output on the client?

0

I tested with SOLO and SERVER. I got that type of output. Why am I not getting one print but two prints and a nil print!? Why? How come? How can I get just one print? This makes no sense. Do help me.

2 answers

local chathandler = {}
--//Events:
local chatAdd = game.Workspace["Remote(s)"].chatAdd;
--//

function chathandler.add(msg)
    print("Executing chathandler.add...");
    chatAdd:FireServer(msg);
end

return chathandler;

Now, you're firing the RemoteEvent from this script. This is a module script. When firing the server, the remote event needs the player variable. Module scripts don't automatically know what the player is, since it's not a local script, so there's no local player. You need to fire the server from a local script, and not a module script. I hope I explained this well, if I didn't please don't downvote, just let me know and I'll add more detail.

1 comment

0

Unfortunately this is not the case. I've tried RemoteEvents w/localscript+modulescript before and have tested that it does know who the LocalPlayer is because when a localscript uses a modulescript, ti becomes an extension of the localscript. I've also tried firing to server directly in the localscript and it yiels the same results.

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