Player.removing not firing

hello all, I’ve been looking at this code block here and i am so beyond lost why not even the output “data saved” is showing up. i tested in studio, in game by looking at the output, and its just not even printing. Can someone explain a reason why it wouldn’t be functioning so I can go in my code and fix it? I don’t even know where to look

game.Players.PlayerRemoving:Connect(function(player)
	print("data saved")	
	local playerKey = "player"..player.UserId
	local data = player.leaderstats.Wins.Value
	dataStore:SetAsync(playerKey, data)
end)

try removing the string and just the player id

I think the main issue is the fact that not even the print statement is running, so something is stopping player.removing from even firing

i mean on the setasync first parameter should be only the player user id
try not adding the ‘player’ string in da playerkey variable

I mean, that could still work but the code doesn’t even get to that part obviously because its not even printing “data saved”

yes cuz the server is removing already than it should function the thing
when its tested in the studio

It depends on what type of script you’re trying to run this through:

  • LocalScripts can only run that’s valid to the Player/Client side
  • Scripts can only run prior if they’re parented inside the workspace/ServerScriptService
  • ModuleScripts & their functions will only run if you’ve called them at the exact right time (Although it doesn’t look like this is one)

Also make sure to turn on API Services if you’re wanting to use the Datastore functions

Use a Script, inside ServerScriptService if you want this to work

game.Players.PlayerRemoving:Connect(function(Plr)
    print("Test")
    local UserId = Plr.UserId
    local Wins = Plr:WaitForChild("leaderstats"):WaitForChild("Wins")

    local success, whoops = pcall(function()
        dataStore:SetAsync("player"..UserId, Wins.Value)
    end)

    if success then
        print("Player's Data for", Plr, "has been saved!")
    else
        warn("Error saving data:", whoops)
    end
end)

Probably this code is not run, does a print statement outside it do anything? Maybe it is in a modulescript that is never imported, or in a function that is never called? Naturally to see it in game, you need to have another person leave to be able to see the message, but I guess you did that.

1 Like

I finally figured it out! my only question is do I need a pcall for the player removing and player adding event to stop errors from happening?

got it! I put a print statement outside to test and apparently the script was never even making it to the part. my guess was because it was after a while true do statement, which I always thought wouldn’t stop the rest of the script from running but in hindsight that makes a lot of sense. Thank you very much!

1 Like

You don’t need to use a pcall because most functions won’t result back as errors unless if you’re handling them the wrong way

You should only use pcalls if a function you’re using is uncertain about it’s usage, and there may be the potential chance of something breaking & entirely ruining the Script (Such as Datastore function fails, or something unexpectingly returning back as nil)

https://www.lua.org/pil/8.4.html

2 Likes