I have a function that doesn’t run when the player leaves the game.
The datastore function does run, so the function is supposed to run too.
game:GetService("Players").PlayerRemoving:Connect(function(plr)
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, plr.Character or plr.CharacterAdded:Wait())
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
print("player left")
end)
The SpawnBackpack function doesnt work nor the print
Any ideas to run fix it or make it so when the player leaves, the function runs?
Possibly an error of the SpawnBackpack function, or an infinite yield (possibly the cause of plr.CharacterAdded:Wait()), or you can try using game.BindToClose instead.
More debug and information is needed so can’t help much, sorry
There is no issue with the function. I ran the function earlier in the code and theres no issue. There is no error (for yield). Doesnt BindToClose launch when the server itself is shutting down?
If I recall correctly, any function connected to BindToClose delays the shutting down of the server for at most a minute to allow the functions to finish any job.
If the functions ended early, so too does the server. If one or more functions did not stop until a minute, however, the server will shut down and the functions won’t be able to finish their jobs.
I mean, even if what you say is true, BindToClose won’t help the issue in my script. I tried leaving the game (with my alt inside) and it didn’t work at all. Plus, the function is ONLY useful if there’s other players in the game, so there’s no point of using BindToClose.
Yea, since you have an if statement return if the profile doesnt exist, we wont know exactly if the event isnt running or its just because of that if statement.
I didn’t use it in my script. I used ProfileService for saving data and I see that there is some issue with when the player leaves the server and comes back (the server says that the player is still in the session so idk maybe there is an issue with PlayerRemoving after all)
I’m not sure if you’re thinking game:BindToClose() is a data saving function, because this is how it’s used:
local Save = function(plr)
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, plr.Character or plr.CharacterAdded:Wait())
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
print("player left")
end)
game:GetService("Players").PlayerRemoving:Connect(Save)
game:BindToClose(Save) -- Bind the save function to when the server is shutting down.
game:GetService("Players").PlayerRemoving:Connect(function(plr)
print("player removing launched")
SpawnBackpack(plr:FindFirstChild("Inventory"), plr, plr.Character or plr.CharacterAdded:Wait())
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
end)
You should have a look at the docs for Player.CharacterAdded
It is an event with a single argument, the Character of the player.
Also, Player.Character can be nil if the player doesn’t have a character, so you should handle that case too.
Finally, you need to handle the case where the player leaves the game while the character is loading (which should be rare).
So, we can rewrite your code as follows: game:GetService(“Players”).PlayerRemoving:Connect(function(plr)
local playerHasCharacter = false
local characterChangedEvent = plr.CharacterAdded:Connect(function(character)
playerHasCharacter = true
characterChangedEvent:Disconnect()
SpawnBackpack(plr:FindFirstChild(“Inventory”), plr, character)
end)
if not playerHasCharacter then
– No character found, so use a timeout to handle the case
– where the player leaves while the character is loading
spawn(function()
wait(2)
if not playerHasCharacter then
– Character wasn’t added after 2 seconds, so it’s probably not going to be added
SpawnBackpack(plr:FindFirstChild(“Inventory”), plr, nil)
end
end)
end
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
print(“player left”)
end)