Using RemoteEvents to find a player in workspace

Hi everyone, I’m using a TextBox and when a player’s name is typed in and entered, the RemoteEvent would fire and the server would try to find the player in the workspace. But I’m having trouble transferring the name of the player from the Local Script to the Server Script.

Local Script:

local UsernameBox = GUI.Frame.ArrestPage.Information.aUsername
local ReplicatedStorage = game:GetService("ReplicatedStorage")

UsernameBox.FocusLost:Connect(function()
	local Username = UsernameBox.Text
	
	if game.Workspace:WaitForChild(Username) then
		ReplicatedStorage.ArrestSystem.Action:FireServer(Username)
	end
	
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.ArrestSystem.Action.OnServerEvent:Connect(function(player, Username)
	if (Username) then
		local player = game.Workspace:WaitForChild(Username)
		print(player)
	end
end)```

So what’s the issue, is it that the RemoteEvent is not firing to the server?

I just don’t know how to grab the input from the TextBox (which is local) to the RemoteEvent

It seems like you’ve done it correctly, are any errors appearing perchance?

1 Like

You’re sending the data from the client to the server correctly.

I would verify the following:

  • Your local script runs (e.g. put a print statement in it)
  • The GUI object is under a PlayerGui and not under StarterGui (a common mistake, try printing GUI:IsDescendentOf(game:GetService("StarterGui"))
  • FocusLost is firing when intended (add a print statement inside)
  • The code inside the wait for child if statement is running (another print statement)
  • Similar things for the server code if nothing above is a problem

(Another note for your code, you might want to verify the child found in workspace is a character. I would recommend instead searching game:GetService("Players") then getting the character with Player.Character (e.g. if a player searches for PineTree and there is a model of a tree named PineTree.)

1 Like

Check where your scripts are running from and their run context. Add some print statements to see where the problem starts.

1 Like

poop doesn’t print when the RemoteEvent is fired. So can you please check if I have fired the RemoteEvent properly?

Local Script:

	local username = GUI.Frame.ArrestPage.Information.aUsername.Text
	local law = LawBroken.Text
	local reason = GUI.Frame.ArrestPage.Information.cReason.Text
	local arrestSystem = ReplicatedStorage:FindFirstChild("ArrestSystem"):FindFirstChild("Action")
	
	arrestSystem:FireServer(username, law, reason)
		
end)```

Server Script:
```local ReplicatedStorage = game:GetService("ReplicatedStorage")
local arrestSystem = ReplicatedStorage:WaitForChild("ArrestSystem"):WaitForChild("Action")

arrestSystem.OnServerEvent:Connect(function(player, username, law, reason)
	print(player, username, law, reason)
end)```

It looks just fine to me. Where is the server script and local script parented to?

Hello, here’s how I would rewrite the code in the local script (I had this problem just yesterday lol). FocusLost returns two params, but only the first one is useful. It is ‘enterpressed : Boolean’

local UsernameBox = GUI.Frame.ArrestPage.Information.aUsername
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ep
repeat 
   ep = UsernameBox.FocusLost:Wait()
until
   ep == true
local Username = UsernameBox.Text
if game.Workspace:FindFirstChild(Username) then
	ReplicatedStorage.ArrestSystem.Action:FireServer(Username)
end

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.ArrestSystem.Action.OnServerEvent:Connect(function(player, Username)
	if Username then
		local player = game.Workspace:WaitForChild(Username)
		print(player)
	end
end)