Why won't this datastore script work?

I have no idea why it won’t work. I’ve looked at tutorials and none of them seem to actually help me. Also, EasyStats is my custom module that creates leaderstats. I’ve tried removing as much of it’s functionality as well, to no avail. I’ve tested in Studio and live games. Please help!
My script:

local EasyStats = require(4350820620)
local ServerStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave")

game.Players.PlayerAdded:Connect(function(Player)
	EasyStats.new(Player, "Bites", 0)
	EasyStats.new(Player, "Rebirths", 0)
	local bites = Player.leaderstats.Bites
	local rebirths = Player.leaderstats.Rebirths
	
	local DataFolder = Instance.new("Folder", ServerStorage.RemoteData)
	DataFolder.Name = Player.Name
	local debounce = Instance.new("BoolValue", DataFolder)
	debounce.Name = "Debounce"
	
	local bitesData, rebirthsData
	
	local success, errormsg = pcall(function()
		bitesData = DataStore:GetAsync("bites-"..Player.UserId)
		rebirthsData = DataStore:GetAsync("rebirths-"..Player.UserId)
	end)
	
	if success then
		if bitesData then
			bites.Value = bitesData
			rebirths.Value = rebirthsData
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local success, errormsg = pcall(function()
		DataStore:SetAsync("bites-"..Player.UserId, EasyStats.Stat("Bites").Value)
		DataStore:SetAsync("rebirths-"..Player.UserId, EasyStats.Stat("Rebirths").Value)
	end)
end)

Also, there are no errors. Help greatly appreciated! :smile:

3 Likes

Requiring modules via the id isn’t valid anymore, you can read about it here:

You’ll need to put the module inside your game and require that.

That’s not quite correct. Only closed-source modules (ones that aren’t take-able) have been removed. The asset is still for sale, so it can be required by ID just fine.

3 Likes

Is your module named “MainModule”? If not, it won’t load into the game.

Can you edit your post to explain exactly how it doesn’t work? Do the stats actually appear in-game? Is the problem with saving? It’s a little vague at the moment.

Also, you’re wrapping your DataStore queries in a protected call, but you’re not printing any errors that might occur. You should add something similar to this after every pcall:

if not success then
    warn(errormsg)
end

It isn’t a problem with the stats (or the module, @CoderCrazy), but the saving. The stat either won’t save or won’t load. When I get home, I’ll include an error script and respond from there.

The module works fine. It’s the datastore part of the datastore script.

Make sure you’re only editing the values on the server (via a Script). Any changes made on the client (via a LocalScript) won’t be saved.

You don’t have any idea why this isn’t working because you’re not applying proper debugging strategies. Chipio already pointed that out to you; you aren’t making full use of pcalls by discarding the error message if one is provided. Your issue is also ambiguous; how is it not working?

If you don’t include these details or to go lengths to debug the code yourself, it’s harder on us to review your code for a potential source of error and it’s difficult on you to receive a solution or learn how to better combat against DataStore failures in the future.

Frankly, there’s a few issues I can see. One is that you have GetDataStore at the top. Given that this is a yielding function, it can cause your code to not register when a player joins. I don’t know if it’s a problem in live games but it has been one before in one environment. Helps to have a catcher for this.

local function onPlayerAdded(player)

end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

Another issue is using two DataStores and a two-for-one pcall. First off, your DataStore calls per join are now raised at 2n where n is the number of players that join. Second is assuming that if success is true and bites have data, then rebirths have data too - these are separate values, you cannot shortcut like this and assume that both exist because one does.