How do you call a remote event in the game.Players.PlayerRemoving function?

Here is my code below. This code saves a string value on a gui when the player leaves and then loads in the saved string value onto the gui when the player rejoins the game. It does this by firing a remote event to the client when the player leaves the game, and another remote event is fired back to the server from the client that has the string text that needs to be saved in its parameter. For some reason, when the player leaves, the string value is not being saved. I tried adding a print line that prints the value saved in the datastore when I rejoined the game, but it just printed nil. Any ideas?

game.Players.PlayerRemoving:Connect(function(player)
game.ReplicatedStorage.Events.PlayerisLeavingFireback.OnServerEvent:Connect(function(player, textstring)
		CoinQuestDatastore:SetAsync(player.UserId, textstring)
	end)
	game.ReplicatedStorage.Events.PlayerisLeaving:FireClient(player)
end)
1 Like

Why do you have a RemoteEvent OnServerEvent in PlayerRemoving? I think you mean to do this?

game.Players.PlayerRemoving:Connect(function(player)
	CoinQuestDatastore:SetAsync(player.UserId, textstring)
	game.ReplicatedStorage.Events.PlayerisLeaving:FireClient(player)
end)
1 Like

Well basically, I need a remote event in playerremoving because the first thing i need to do is fire a remote event to the client telling the client that the player has left. Then, the client fires a remote back to the server giving the server the text string that needs to be saved in the parameter of the remote. Then, the server saves this text string and then thats it.

1 Like

In that case can’t you put the event outside of the PlayerRemoving? If you have it set up so the Client fires it back to the server, you don’t need it in there I believe, you’d just have to do

game.ReplicatedStorage.Events.PlayerisLeavingFireback.OnServerEvent:Connect(function(player, textstring)
	CoinQuestDatastore:SetAsync(player.UserId, textstring)
end)

game.Players.PlayerRemoving:Connect(function(player)
	game.ReplicatedStorage.Events.PlayerisLeaving:FireClient(player)
end)

And it would work for what you want I believe. And if this still doesn’t fix that issue, what is the code that you have in the OnClientEvent?

1 Like

Here is what I have in my OnClientEvent. I don’t believe I can put the event outside of PlayerRemoving because I want to save the data when the player leaves, which is inside the event PlayerRemoving.

game.ReplicatedStorage.Events.PlayerisLeaving.OnClientEvent:Connect(function()
	game.ReplicatedStorage.Events.PlayerisLeavingFireback:FireServer(script.Parent.Frame.MainFrame.Quest1.CoinAmount.Text, player)
end)
1 Like

I think you can still have it outside of your event and it would still work, although the only issue that it could be is taht there’s not enough time between firing the events and the player finally leaving. I don’t think you’d even need events since you can just use player.PlayerGui and just navigate to the thing you want to store into the datastore, all completely from the server.

1 Like

Why don’t you just fire to the server when the player knows they’re leaving on the client instead?

game.Players.PlayerRemoving:Connect(function(plr)
	if plr == game.Players.LocalPlayer then
	game.ReplicatedStorage.Events.PlayerisLeavingFireback:FireServer(script.Parent.Frame.MainFrame.Quest1.CoinAmount.Text, player)
	end
end)
2 Likes

I see multiple problems with what you are trying to attempt.

To start off, if I understood correctly, after the PlayerRemoving event is being fired, you then try to fire a separate remote to that client that has already left, requesting for information about their GUI? I would assume that trying to request from a client that has already closed the game could cause problems, but am not sure if it is directly related to what you are doing.

Since this made me curious, I just went and tested it. It seems like the remote event just never fires. My test consisted of this:

Server Script in ServerScriptService

game.Players.PlayerRemoving:Connect(function(player)
	game.ReplicatedStorage.RemoteEvent:FireClient(player)
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, NumberToPrint)
	print(NumberToPrint)
end)

LocalScript as a child of a TextLabel in a ScreenGui in StarterGUI

script.Parent.Text = math.random(1,10)

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
	game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Text)
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player == game.Players.LocalPlayer then
		game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Text)
	end
end)

Requesting from the client on leave did not cause the event to print anything. Connecting PlayerRemoving from a client localscript, as @Nodehayn says, also did not activate the remote and print anything. Therefore, I am assuming your DataStore is printing nil because the event never fires, therefore never saving anything into the DataStore. You will need to find a new method of retrieving the GUI information from the player before they leave the game.

1 Like

I tried the script that you provided and put it in my local script under my Gui, but it still doesn’t work. Correct me if I’m wrong, but doesn’t game.Players.PlayerRemoving only work in server scripts? (I might be wrong)

It works on either end. I think it doesn’t work because when the client leaves, at that point every connection on the client is destroyed (all the localscripts and whatnot).

2 Likes

Is there a way to access the Gui in the server script? That way I may not need to use any remotes.

Uh, IIRC that you can access the PlayerGui of a player on the server, I don’t remember exactly though, you’d have to give it a try.

Yes, you can directly access the Player’s GUI without using any remotes. However, if the changes to the textlabel are made through a localscript, the server will not see the changes made. Then the TextLabel’s text will stay as Label. I tested that at first and it seems client GUI changes do not replicate.

1 Like

I think he should just fire to the server each time the client updates the text and keep track of the text instead if he can’t do anything else.

1 Like

There are a couple of different things you could try. I don’t know the nature of what you’re making so it’s a little hard to say ahead of time. But, you can make a remote that fires whenever you change the text of the textbox. What causes the textbox text to change? Think about that, and bind that to a remote event. On the server side, you can store a dictionary of all the player’s textbox strings. When the remote event fires, change the stored string. When the player leaves the game, save the stored string associated with their ID, and then remove them from the table.

I also think this would be the solution here.

1 Like

I tried that already, but it is very likely that the datastore requests queue would fill up very quickly every time the player touches a coin.

Basically, I am trying to create a quest where the player has to collect 10 coins that are spread out around the map. When the player touches a coin, the text changes from 0/10 to 1/10, and so on. When the player leaves, I want the datastore to save whatever the text was, so that when the player rejoins the game, they can continue from what they were at before and not lose progress.

You are not supposed to save every time the player’s text changes. You save only when the player leaves the game, but keep track of it until they leave on the server and with remote events.

Now that you mention players touching a coin though, this can be done entirely from the server. You do not need the client’s input here. Not only that, but requesting from the client how many coins they have is extremely unsecure and can be easily exploited to get the desired number of coins. Never trust the client, especially with a sensitive stat such as this.

This can be done a lot easier without remotes, tables, etc. You should keep an IntValue object in the player. Instead of detecting from the client when they touch the coin, detect it from the server. Most probably using Coin.OnTouched and finding which player touched the coin. After that, increment the IntValue object stored in the player. When they leave the game, read what value the IntValue has and save that into the Datastore. For changing the text, I believe you can change the text for that player directly from the server by accessing their PlayerGui. If that doesn’t work, fire a remote in the same section that the OnTouched and Incrementing of the stat is done.

2 Likes

I will try this, but I don’t think the IntValue in the player will be necessary. I can just save whatever the text is into the datastore when the player leaves. (Since datastores can save strings too, and not just number values)