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

How do I find a Player from game.Players in a server script?

I am making this SurfaceGui that when you click it opens up a TextBox (this is all local). I want to be able to trasnfer whats in that TextBox to a TextLabel in Workspace for the whole server to be able to see. I know I can't use LocalPlayer in a server script, so how else would I be able to do this? (This is for FE, so its a remote event)

Local script:

local RemoteEvent = game.ReplicatedStorage.NameForOrder

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	wait(0.1)
	script.Parent.Parent.Parent.Parent.SurfaceGui.Main.Visible = true
	wait(0.1)
	RemoteEvent:FireServer()
end)

Sever script:

local RemoteEvent = game.ReplicatedStorage.NameForOrder

RemoteEvent.OnServerEvent:Connect(function(player)
	local textbox = player.PlayerGui.ScreenGui.Frame.TextBox
	game.Workspace.OrderMachine.Order.SurfaceGui.Frame.TextLabel.Text = "Order for "..textbox.Text..":"
end)

1 comment

0

It didn't work because I said worksapce;) try again

1 answer

This would be simple, use the Players:GetPlayers() function, and nest it into an array. If you'd like to access them you should use a for loop, and add a Conditional Statement to rule out every other player.

You can also pass the LocalPlayer's name and use it as a parameter for an argument, yet I think this would all be unnecessary as Firing the Server will pass whoever Fired the Remote, as you already have written, but if you'd like to work with a different player, sent from the Remote? then it would look somewhat like so

RemoteEvent.OnServerEvent:Connect(function(FiredPlayer, LocalPlayer)
	local Players = game:GetService("Players"):GetPlayers()
	for Index, Player in ipairs(Players) do
		if (LocalPlayer == LocalPlayer and LocalPlayer.Character) then
			print(LocalPlayer.."!")
		end
	end
end)

You can also write it in this form too.

RemoteEvent.OnServerEvent:Connect(function(FiredPlayer, LocalPlayer)
	local Players = game:GetService("Players"):GetPlayers() --// Get acces to all Players.
	local Player = nil --// Player you're working with.
	local function CheckForPlayer (Player) --// Argument.
		for Index, Player in ipairs(Players) do --// Run through all Players
			if (LocalPlayer.Name ~= LocalPlayer and not LocalPlayer.Character) then --// Ignore if not the specified Player
				return false
			end
			Player = Player --// Set the Player variable for outside referencing.
			return true
		end
	end
	if (CheckForPlayer("JxkeBape")) then --// Run the function tied in a Conditional Statement
		print(Player.."!")
	end
end)

That's for other terms, but I believe this is what you're probably looking for, this should be mostly done Client-side. You're using some parts in the wrong place, try this?

--//Client
local Player = game:GetService("Players").LocalPlayer
local PlayerGUI = Player:WaitForChild("PlayerGui")
local TextBox = PlayerGUI:WaitForChild("TextBox")

local Remote = game:GetService("ReplicatedStorage").Remote

TextBox.FocusLost:Connect(function(Return) --// Event that fires when a TextBox was left, provides parameter of if Return pressed.
	if (Return) then
		local Text = TextBox.Text
		Remote:FireServer(Text)
	end
end)

--//Server
local Remote = game:GetService("ReplicatedStorage").Remote
local TextLabel = workspace:WaitForChild("TextLabel")
Remote.OnServerEvent:Connect(function(Player, Text)
	TextLabel.Text = Text
end)

Hope this helps, I do suggest finding another method but If these work for you too then that's perfect! Good Luck

4 comments

0

I used flat examples, there are references in here that aren't named accordingly, you can fix that up on your end, hope it works out, i didn't quite get your question so I provided multiple examples, Good Luck

0

Why don't you just index the player that fired the remote which is automatically passed and then pass the text you want to send???

0

Amend the code above to fit with whatever you're working with

0

For some reason the last code you made didn't work and I fitted it to everything

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