How to i fix this?

Hello Devs,
I have this line of code but i dont know how to get the local player in a normal script.

local Showevent = game.ReplicatedStorage.Showevent
local Set = game.Players.Localplayer.Settings.ShowFrame
Showevent.OnServerEvent:Connect(function()
    game.StarterGui.ScreenGui.TextLabel.Visible = true
    game.StarterGui.ScreenGui.TextLabel.Text = "YES"
    Set.Value = true
end)

Any help is appreciated

1 Like

You cannot get the LocalPlayer in a server side script. That is a property restricted to LocalScripts.

3 Likes

Showevent.OnServerEvent:Connect(function(plr)

plr will be equal to the player who fired the event.

5 Likes

In the parentheses in function(), you can add plr to get the player that fired the event.

2 Likes

Thanks for all the feed back I understand what a did wrong.

1 Like

Hey there! Hopefully this is a solution to your script.

game.Players.PlayerAdded:Connect(function(plr))
local Showevent = game.ReplicatedStorage.Showevent
local Set = plr.Settings.ShowFrame
Showevent.OnServerEvent:Connect(function()
    game.StarterGui.ScreenGui.TextLabel.Visible = true
    game.StarterGui.ScreenGui.TextLabel.Text = "YES"
    Set.Value = true
end)
end)

If this works out you may put this as a solution

2 Likes

Each RemoteEvent (and RemoteFunction) on server, provides you with the player that has fired this event (or invoked function).

Showevent.OnServerEvent:Connect(function(PlayerThatFired)

However Your script will not work as desired. Changing values in StarterGui will not change anything for players already in the game (unless they reset) only for new players.
To change values in specific player gui use

Showevent.OnServerEvent:Connect(function(PlayerThatFired)
    PlayerThatFired.PlayerGui.ScreenGui.TextLabel.Visible = true
    PlayerThatFired.PlayerGui.ScreenGui.TextLabel.Text = "YES"
    Set.Value = true
end)

I hope this helps.

p.s. If you need to change player GUI, there is no need to use event. Simply use LocalScript to access player GUI, as the server does not need to know about those changes anyway (in most cases).

1 Like

So if you want to get the local player (not the same as the player who fired the event), you can do a PlayerAdded function.

It should be like this:

local Showevent = game.ReplicatedStorage.Showevent

--// Logical script

game.Players.PlayerAdded:Connect(function(localplr)

  local Set = localplr.Settings.ShowFrame
  Showevent.OnServerEvent:Connect(function()
      localplr.PlayerGui.ScreenGui.TextLabel.Visible = true
      localplr.PlayerGui.ScreenGui.TextLabel.Text = "YES"
      Set.Value = true
  end)
end)

I added the PlayerAdded function which gets the player. Then the script will find the player’s PlayerGui and the ScreenGui.

Hope it helps.

2 Likes

Hey there! Hopefully this is a solution to your script.

game.Players.PlayerAdded:Connect(function(plr))

So if you want to get the local player (not the same as the player who fired the event), you can do a PlayerAdded function.

Please stop. Doing it this way will cause new function being made every time new player joins. This function will then run multiple times, (depending on the amount of players in the server), each time a SINGLE players invokes the function.
While it may work, this is a bad habit and should not be encouraged.

1 Like