Issue with calling a function from a ModuleScript

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! :slight_smile:

I don’t know if people will be able to help much without a code snippet from your module. Given it’s likely an issue inside the module itself.

Alright, I’ll add the snippet from the module. Thanks!

1 Like

When you use pcalls, it is a best practice to print a warning to the output if they fail. This usually makes debugging issues much easier. Does the “values saved” string print? How about the pet names and success message at the end?

1 Like

None of those print. Also, thanks for the advice!

If your post-function print doesn’t work and you got no error output, then it’s probably that your code is hanging. Do you happen to get an “infinite yield possible” warning?

Check that anything you wait for actually exists (note that if you make the script yield it’s possible the player is removed before you’ve finished with them, so things like player:WaitForChild(“Pets”) may never return anything).

My guess for the root cause:

  • I noticed you access ReplicatedStorage[ player.Name ] and also ReplicatedStorage.Players[ player.Name ] — is that correct? Do both folders named the player exist? Or did you do a typo on rPlayer definition?
2 Likes

Omg, yes thanks so much! The typo was holding it back from working :joy:

EDIT: I was wondering what was wrong, I checked the script so many times to find nothing was wrong but didn’t see the typo. Again, thanks!

1 Like
function Whatever()
--code
end

local Do = pcall(Whatever)
if Do == false then
print("Error with function")
end
1 Like