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

Am I using this RemoteEvent correctly?

The point of this script is accepting an Input on a gui and setting it as a billboard Gui over a player's head.

This is the LocalScript calling on the event.

script.Parent.MouseButton1Down:connect(function()
	game.Workspace.NamingEvent:FireServer()
end)

This is the Event, this isn't working in game

local event = Instance.new("RemoteEvent")
event.Name = "NamingEvent"
event.Parent = game.Workspace
event.OnServerEvent:connect(function()
	local Detect = script.Parent.FocusLost
	Detect:connect(function()
		local player = game.Players.LocalPlayer
		local chat = game:GetService('Chat')
		if script.Parent.Text ~= "" then
			player.Character.Head.BillboardGui.MainFrame.PlayerLabel.Text = chat:FilterStringForBroadcast(script.Parent.Text, player)
			player.leaderstats.Character.Value = chat:FilterStringForBroadcast(script.Parent.Text, player)
			player.Character.Head.BillboardGui.MainFrame.PlayerName.Text = player.Name
		end
	end)
end)

1 answer

✓ Accepted answer

Well, partially, but you have some problems in your code. Let's walk through this.

First off, make sure that the client isn't trying to fetch an object not created yet. To solve this, make a RemoteEvent manually and put it in ReplicatedStorage, and for even more safety you can add wait(1) at the top of the local script or do something with WaitForChild

Now onto the big issue I see:

Looking in this code I see that you are using "LocalPlayer" which cannot be used in a server script.

Instead, do it like this: (make sure to use :Connect as :connect is deprecated) Also, it is better to put the even in ReplicatedStorage, that way it doesn't get lost in workspace, and can be accessed by the server and client.

local event = game.ReplicatedStorage.SomeEvent;
event.OnServerEvent:Connect(function(player, data) 
       -- Do something with player.
end);

To fire this event:

local event = game.ReplicatedStorage.SomeEvent;
script.Parent.MouseButton1Down:Connect(function()
    event:FireServer(WhatEverDataYouNeedToSendGoesHere)
end)

And there is no need to send the player with the data, as the server already receives that as it's first parameter.

Now, It seems like your trying to make some sort of chat system. I won't go full blown here and recede it all to work for you, plus I don't even know if that's your entire script but here is an example:

Server:

local chat = game:GetService('Chat');
local event = game.ReplicatedStorage.SomeEvent;
event.OnServerEvent:Connect(function(player, message) 
       player.Character.Head.BillboardGui.MainFrame.PlayerLabel.Text = 	
       chat:FilterStringForBroadcast(message, player);
end);

Client:

local event = game.ReplicatedStorage.SomeEvent;
local someTextInput = script.Parent.Parent.SomeTextInput; -- This is an example, you will need to replace this with whatever you are actually trying to grab input from.

script.Parent.MouseButton1Down:Connect(function()
       event:FireServer(someTextInput.Text);
end);
someTextInput.FocusLost:Connect(function()
       event:FireServer(someTextInput.Text);		
end);

This should work provided you know how to finish connecting it, I assume script.Parent is a button and just as an example script.Parent.Parent.SomeTextInput would be the textbox?

My point is that aside from a few issues with actually using RemoteEvents, the main problem is that you are trying to access things from the client server-side, which doesn't work.

My advice is to read this tutorial on how to create a custom chat: http://wiki.roblox.com/index.php?title=Custom_chat_GUI

And read more on RemoteEvents: http://wiki.roblox.com/index.php?title=Remote_Events_and_Functions

It will also be helpful to read about Filtering Enabled: http://wiki.roblox.com/index.php?title=FilteringEnabled

I know this was kind of lengthy but I hope it helps!

13 comments

1

Thankyou so much for your response, I'll try it out as soon as I have the time and come back if I have any questions?

0

Sure thing, If it fixed the issue be sure to mark the question as answered, let me know if you need anything else :)

1

I want to thank you for your answer, it's the most useful thing I've found on this site so far. You didn't assume that I'm a complete genius or just link me to a page in the wiki which I would have already found. Your explanation was a lot clearer to me than any other sources I could have found. Keep this up! It was fantastic

0

There's only one problem, it's helped my understanding a lot but it still didn't fix it. The script still won't work ingame- I wonder if I've implemented it incorrectly

0

Mind telling me if the error log is showing anything?

0

As I said it was more of a sample code for you to work with so I don't expect it to work 100%, but if its giving an error its possible I may have made a typo somewhere

0

I tried out putting all the events in ReplicatedStorage, and it looked ideal. However when I input anything it just won't react. The event just can't be firing, or I suspect it's an error with passing the text across to the event?

0

aha! "MouseButton1Down is not a valid member of TextBox"

0

also an infinite yield error on 'WaitForChild('Customizer')

0

Alright, so it looks like your problem is solved?

0

Well I don't know what I should use in place of MouseButton1Down, MouseButton1Click didn't work either. I also havn't the foggiest idea how to fix this WaitForChild error, I'm looking into it

0

TextBoxes don't detect mouse events, only mouse enter and leave. If you want to detect the user pressing enter you can use: http://wiki.roblox.com/index.php?title=Keyboard_input or use MouseButton1Click on a button.

1

Thankyou, FocusLost works perfectly. The only problem now is a separate script with similar issues but I'll write it up as a different question. Thankyou for all your help

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