Need help with datastore script

Great, that adds +1 to datastore scripts that dont work

Okay, so I’m trying to make a datastore module (for the 15th time) and I’ve gotten to the point where it doesn’t work.
Now obviously, I have no idea what I’m doing, help is needed

local dsService = game:GetService("DataStoreService")
local PlayerDataDS = dsService:GetDataStore("PlayerData")
local PlayerPlus = {}

PlayerPlus.PlayerData = {}
PlayerPlus.PlayerCache = {}

local function PlayerAddedLdrst(Instancetype,name: string,plr: Player): ValueBase
	local ldrst = plr.leaderstats
	local returnedValue = Instance.new(Instancetype) :: ValueBase
	returnedValue.Parent = ldrst
	returnedValue.Name = name
	return returnedValue
end

function PlayerPlus:Setup(plr: Player)
	PlayerPlus.PlayerData[plr] = 
		{
			Data =
			{
				currency = 
				{
					coins = PlayerAddedLdrst("IntValue","Coins",plr).Value;
				}
			};
			DataStore = PlayerDataDS;
			DataStoreName = "PlayerData";
			Key = plr.UserId;
		}

	PlayerPlus.PlayerCache[plr] =
		{
			gamepasses = 
			{
				twoTimesMoney = false;
			}
		}
	local success, PlayerDataVar = pcall(function()
		return PlayerDataDS:GetAsync(PlayerPlus.PlayerData.Key)
	end)
	PlayerPlus.PlayerData[plr].Data.currency.coins = (type(PlayerDataVar) == "number" and PlayerDataVar) or 0
end

function PlayerPlus:SaveData(plr: Player)
	for i,v in PlayerPlus.PlayerData[plr].Data do
		PlayerDataDS:SetAsync(PlayerPlus.PlayerData[plr].Key,PlayerPlus.PlayerData[plr].Data)
	end
end

return PlayerPlus

Server:

local PlayerPlus = require(script.Parent.PlayerPlus)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local ldrst = Instance.new("Folder")
	ldrst.Parent = plr
	ldrst.Name = "leaderstats"
	PlayerPlus:Setup(plr)
end)

Players.PlayerRemoving:Connect(function(plr)
	PlayerPlus:SaveData(plr)
end)

Have you tried using print statements to locate where in the script the issue is.

When I do print(PlayerDataVar) it says "arguement 1 missing or nil

Your “PlayerDataVar” is in a pcall() and is the error message if not success. Here is the Pcall Documentation

It works now, thanks!

charrrrrrrrr

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.