Im trying to make a remote function that removes a players name from the leaderboard(surfacegui) when they leave. Problem is it removes the wrong players name and when i try to print the players name it also prints the wrong name:
https://gyazo.com/81c6d71e6d5addfe265a072c5a347702
Client
local Players = game:GetService("Players") game.Players.PlayerRemoving:connect(function() game.Workspace.Leave.Go:InvokeServer() end)
Server
script.Go.OnServerInvoke = function(player) print (player.Name) for index, child in pairs(workspace.LeaderBoard.SurfaceGui.Frame:GetChildren()) do if child.Name == "Player1" then print ("Player1Left") end end end
Well for starters, there are a few problems in your script that can easily be fixed(Luckily I'm here to help you do it :D).
For starters, you're editing a GUI on the Server Side when you should be doing it on the Client Side(especially if you're using Filtering Enabled[soon to be called Non-experimental mode]).
To fix this, instead of having the client ask the server to update the list, you should do the opposite, which would look a little bit like this:
--Server game:GetService("Players").PlayerRemoving:Connect(function(Player) print(player.Name.." is leaving the server.") for _,p in pairs(game:GetService("Players"):GetPlayers()) do if p ~= Player then --Telling a player to do something when they're leaving? script.Go:InvokeClient(p.Name) end end) end)
Now we have the server calling the shots instead of the client, however the client what to do! Which is why we have to tell it by adding a callback to the RemoteFunction
, which would look a little bit like this:
--Client workspace.Leave.Go.OnClientInvoke = function(whoLeft) for _,l in pairs(workspace.Leaderboard.SurfaceGui.Frame:GetChildren()) do if l.Name == whoLeft then print("This dude left! Mollywopping his name off my list.") l:Destroy() end end end
The confused client knows what it's doing, and evolves into the smart client! Now when the player leaves, the client does all the work(which it's supposed to when it comes to gui work), and the server just acts as the messenger! Neat, right?
ReplicatedStorage
for easier access.Apparently you can do it without remote events or functions :P
Server
game:GetService("Players").PlayerRemoving:Connect(function(Player) print(Player.Name.." is leaving the server.") for _,p in pairs(game:GetService("Players"):GetPlayers()) do if p ~= Player then --Telling a player to do something when they're leaving? for index, child in pairs(workspace.LeaderBoard.SurfaceGui.Frame:GetChildren()) do if child.Name == Player.Name then child:Destroy() print("complete") end end end end end)