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

Why wont my remote functions work properly?

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

1 comment

0

You never passed the player as an argument in the local script.

2 answers

✓ Accepted answer

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).


#Problem #1(and #2 kinda)

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?


#Notes

  • Instead of having your RemoteFunctions/RemoteEvents inside of the server script, move them to ReplicatedStorage for easier access.
  • Do note: When doing GUI work, all work should be done on the client instead of the server, ESPECIALLY when using FE.
  • I wrote this at 1 in the morning, so sorry if I sound a bit loopy.
  • All code was written in the website answer box, so if I made a mistake, please tell me and I will fix it immediately.

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)

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