Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get a players name in the server script?

Asked by 6 years ago

Local script

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local remote2 = repStorage:WaitForChild("Remote2")
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:connect(function()

    script.Parent.Parent.Parent.Joined.Visible = true
    script.Parent.Parent.Parent.ServerList.Visible = false
    if game.StarterGui.Menu.Joined.Name1.Text == "" then

            remote:FireServer()

    else
        if game.StarterGui.Menu.Joined.Name2.Text == "" then

            remote2:FireServer()

        else
            game.StarterGui.Menu.ServerList.Full.Visible = true
            wait(2)
            game.StarterGui.Menu.ServerList.Full.Visible = false

            end
        end

end)

Server script (Workspace)

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Remote")
local remote2 = repStorage:WaitForChild("Remote2")


remote.OnServerEvent:connect(function()
                game.StarterGui.Menu.Joined.Name1.Text = game.Players.LocalPlayer.Name -- I don't know how to get the players name who clicked the Textbutton?
        game.StarterGui.Menu.ServerList.Name1.Text = game.Players.LocalPlayer.Name 
end)

remote2.OnServerEvent:connect(function()

game.StarterGui.Menu.Joined.Name2.Text = game.Players.LocalPlayer.Name
            game.StarterGui.Menu.ServerList.Name2.Text = game.Players.LocalPlayer.Name
end)

1 answer

Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

The first argument sent via remotes is the player's player instance. This is the only trustworthy argument from the client with regards to remotes, as it's verified by the player's authentication-token, so players can't pretend to be someone else. The player parameter is passed implicitly, meaning that with this code client-side:

RemoteEvent:FireServer("A string!")

You could handle it like this server-side:

RemoteEvent.OnServerEvent:Connect(function(Player, SpecialString)
    print(Player.Name .. " sent us the string: " .. SpecialString)
end)

In this case, remotes are besides the point. You're modifying game.StarterGui and trying to modify the GUIs server-side. It's important to remember three things:

  1. GUIs parented client-side won't replicate to the server (including GUIs parented automatically from game.StarterGui.
  2. The server should never care about GUIs, it's not its responsibility. It should instead use remotes to tell the client when things happened, and the client should be the one manipulating GUIs.
  3. game.StarterGui is merely the template for GUIs to be cloned from into Player.PlayerGui. Modifying it probably isn't what you want.

I recommend setting the ScreenGui.ResetOnSpawn property of this GUI to false. After that, you'd want to use RemoteEvents to inform all other players of changes to GUIs that you would want to make. You could use the game.Players.PlayerAdded event client-side if you want to modify a GUI whenever a player joins the game.

RBXScriptSignal:connect() is deprecated, you should prefer RBXScriptSignal:Connect().

Ad

Answer this question