Hello, developers.
I am updating my DataStore to make it tidier, but for some reason when I try to call a function to save data when the player leaves from a module script, it doesn’t work. I’ve also tried printing to see where it stops.
PlayerRemoving Event
local DataModule = require(game:GetService("ReplicatedStorage").DataModule)
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function(plr)
wait()
print("player leaving")
DataModule.SaveData(plr)
print("data is saving")
end)
ModuleScript function
local PlayerData = game:GetService("DataStoreService"):GetDataStore("Player_Data9")
local ds = game:GetService("DataStoreService"):GetDataStore("PetsData11")
local ds2 = game:GetService("DataStoreService"):GetDataStore("BackpacksOwned2")
local ds3 = game:GetService("DataStoreService"):GetDataStore("PetsDiscovered1")
module.SaveData = function(player)
local Key = player.UserId
local rPlayer = game:GetService("ReplicatedStorage"):WaitForChild(player.Name)
pcall(function()
local ValuesToSave = {
cash = player.leaderstats.Cash.Value,
sugar = player.leaderstats.Sugar.Value,
cottonCandy = player.leaderstats["Cotton Candy"].Value,
inventory = player.Inventory.Value,
rebirths = player.leaderstats.Rebirths.Value,
cooldown = rPlayer.Cooldown.Value,
backpackEquipped = rPlayer.BackpackEquipped.Value,
toolEquipped = rPlayer.ToolEquipped.Value
}
PlayerData:SetAsync(Key, ValuesToSave)
print("values saved")
end)
--Pets
pcall(function()
local key = "pets-"..player.UserId
local petsToSave = {}
for i,v in pairs(player:WaitForChild("Pets"):GetChildren()) do
table.insert(petsToSave, v.Name)
print(v.Name)
end
ds:SetAsync(key, petsToSave)
end)
--Backpacks
pcall(function()
local key = player.UserId
local backpacksToSave = {}
for i,v in pairs(game.ReplicatedStorage.Players:WaitForChild(player.Name):WaitForChild("BackpacksOwned"):GetChildren()) do
table.insert(backpacksToSave, v.Name)
end
ds2:SetAsync(key, backpacksToSave)
end)
--Pets Discovered
pcall(function()
local key = player.UserId
local petsToSave = {}
for i,v in pairs(game.ReplicatedStorage.Players:WaitForChild(player.Name):WaitForChild("PetsDiscovered"):GetChildren()) do
table.insert(petsToSave, v.Name)
end
ds3:SetAsync(key, petsToSave)
end)
print("Player Data saved successfully. (" .. player.Name .. ")")
game:GetService("ReplicatedStorage").Players:FindFirstChild(player.Name):Destroy()
end
Nothing saves. I check the output and “player leaving” prints but “data is saving” does not. There were also no errors in the output.
Any suggestions are greatly appreciated! ![]()