Need Help WIth Data Stores

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i need these values in the script to be saved
  2. What is the issue? Include screenshots / videos if possible!
    it won’t save
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    ive tried to use update async but that didn’t work
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local Players = game:GetService("Players")

-- Data Store's -- 
local DataStoreSerivce = game:GetService("DataStoreService")
local UserData = DataStoreSerivce:GetDataStore("UserData")

-- Over Stuff -- 
local KeyPrefix = "Player: "

-- Functions -- 

local function Save(plr: Player)
	local Key =  KeyPrefix..tostring(plr.UserId)
	local StuffWeNeed = {
		Muilt = 0,
		Dollars = 0,
		MustHos = "None"
	}

	local Dollars: NumberValue = plr.Leaderstats.Dollars
	local Muilt: NumberValue = plr.Leaderstats.Muilt
	local MushHos:StringValue = plr.Leaderstats.MustHos
	Dollars.Changed:Connect(function(CurrentNumber)
		StuffWeNeed.Muilt = CurrentNumber
	end)
	Muilt.Changed:Connect(function(CurrentNum)
		StuffWeNeed.Dollars = CurrentNum
	end)

	MushHos.Changed:Connect(function(NowHos)
		StuffWeNeed.MustHos = NowHos
	end)
	local Success, ErrorMsg 
	repeat
		wait()
		Success, ErrorMsg = pcall(function()
			UserData:SetAsync(Key, StuffWeNeed)

		end)
	until Success
	if not Success then
		warn("Unable To Save Data "..plr.UserId.." "..tostring(ErrorMsg))
	end
end

local function Load(plr: Player)
	local Key = KeyPrefix..tostring(plr.UserId)
	local Success, ErrorMsg
	local Data

	repeat
		task.wait()
		Success, ErrorMsg = pcall(function()
			Data = UserData:GetAsync(Key, Data)
		end)
	until Success or not Players:FindFirstChild(plr.Name)
	if not Data then 
		local Inventory = Instance.new("Folder", plr)
		Inventory.Name = "Inventory"

		local Leaderstats = Instance.new("Folder", plr)
		Leaderstats.Name = "Leaderstats"

		local Dollars = Instance.new("NumberValue", Leaderstats)
		Dollars.Name = "Dollars"
		Dollars.Value = 5000


		local Muilt = Instance.new("NumberValue", Leaderstats)
		Muilt.Name = "Muilt"
		Muilt.Value = 1

		local MustHos = Instance.new("StringValue", Leaderstats)
		MustHos.Name = "MustHos"
		MustHos.Value = "None"
	end

	if Success then



		local Inventory = Instance.new("Folder", plr)
		Inventory.Name = "Inventory"

		local Leaderstats = Instance.new("Folder", plr)
		Leaderstats.Name = "Leaderstats"

		local Dollars = Instance.new("NumberValue", Leaderstats)
		Dollars.Name = "Dollars"


		local Muilt = Instance.new("NumberValue", Leaderstats)
		Muilt.Name = "Muilt"

		local MustHos = Instance.new("StringValue", Leaderstats)
		MustHos.Name = "MustHos"

		for i, Val in ipairs(Data) do
			Dollars.Value = Val[1]
			Muilt.Value = Val[2]
			MustHos.Value = Val[3]
		end
	end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)
game:BindToClose(function()
	for i,v in ipairs(game:GetService("Players"):GetPlayers()) do
		Save(v)
	end
end)
-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
Please help ive spent 2 hours trying to fix.

This should fix it

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local UserData = DataStoreService:GetDataStore("UserData")

local KeyPrefix = "Player:"

local function Save(plr)
    local Key = KeyPrefix .. tostring(plr.UserId)
    local StuffWeNeed = {
        Multi = plr.Leaderstats.Multi.Value,
        Dollars = plr.Leaderstats.Dollars.Value,
        MustHos = plr.Leaderstats.MustHos.Value
    }

    local Success, ErrorMsg
    repeat
        wait()
        Success, ErrorMsg = pcall(function()
            UserData:SetAsync(Key, StuffWeNeed)
        end)
    until Success
    if not Success then
        warn("Unable To Save Data " .. plr.UserId .. " " .. tostring(ErrorMsg))
    end
end

local function Load(plr)
    local Key = KeyPrefix .. tostring(plr.UserId)
    local Success, ErrorMsg, Data

    repeat
        task.wait()
        Success, ErrorMsg = pcall(function()
            Data = UserData:GetAsync(Key)
        end)
    until Success or not Players:FindFirstChild(plr.Name)

    if not Data then
        if not plr:FindFirstChild("Inventory") then
            local Inventory = Instance.new("Folder", plr)
            Inventory.Name = "Inventory"
        end

        if not plr:FindFirstChild("Leaderstats") then
            local Leaderstats = Instance.new("Folder", plr)
            Leaderstats.Name = "Leaderstats"

            local Dollars = Instance.new("NumberValue", Leaderstats)
            Dollars.Name = "Dollars"
            Dollars.Value = 5000

            local Multi = Instance.new("NumberValue", Leaderstats)
            Multi.Name = "Multi"
            Multi.Value = 1

            local MustHos = Instance.new("StringValue", Leaderstats)
            MustHos.Name = "MustHos"
            MustHos.Value = "None"
        end
    end

    if Success then
        for i, Val in ipairs(Data) do
            plr.Leaderstats.Dollars.Value = Val.Dollars
            plr.Leaderstats.Multi.Value = Val.Multi
            plr.Leaderstats.MustHos.Value = Val.MustHos
        end
    end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

Make sure API services are turned on in that Roblox game

it doesn’t create the folder inside of the player for some reason for inventory or Leaderstats

do any errors pop up at all? character limit

Do this in Roblox studio by clicking settings and go to security.

If its already turned on then put some prints and warns throughout the script and is there any errors in output?

the api is already turn on and it just doesn’t make the folders


this is the output after leaving the game

It’s probably because your loop in getting the data is yielding because if a player doesn’t have any data saved at all, it will loop infinitely, so that’s why the Inventory folder and Leaderstats folder will never be created. I suggest adding limited number of retries, let’s say 10.

	local Key = KeyPrefix..tostring(plr.UserId)
	local Success, ErrorMsg
	local Data
	
	local retries = -1 -- i made this -1 because if i made it zero, the loop will stop in 9 retries instead of 10
	repeat
		Success, ErrorMsg = pcall(function()
			Data = UserData:GetAsync(Key, Data)
		end)
		task.wait()
		retries += 1
	until Success or not Players:FindFirstChild(plr.Name) or retries >= 10

the problem with that script is i think is that it only makes the Inventory and that stuff if the player has no data

Give this one a try:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local UserData = DataStoreService:GetDataStore("UserData")

local KeyPrefix = "Player:"

local function Save(plr)
    local Key = KeyPrefix .. tostring(plr.UserId)
    local StuffWeNeed = {
        Multi = plr.Leaderstats.Multi.Value,
        Dollars = plr.Leaderstats.Dollars.Value,
        MustHos = plr.Leaderstats.MustHos.Value
    }

    local Success, ErrorMsg
    repeat
        task.wait()
        Success, ErrorMsg = pcall(function()
            UserData:SetAsync(Key, StuffWeNeed)
        end)
    until Success
    if not Success then
        warn("Unable To Save Data " .. plr.UserId .. " " .. tostring(ErrorMsg))
    end
end

local function Load(plr)
    local Key = KeyPrefix .. tostring(plr.UserId)
    local Success, ErrorMsg, Data

    repeat
        task.wait()
        Success, ErrorMsg = pcall(function()
            Data = UserData:GetAsync(Key)
        end)
    until Success or not Players:FindFirstChild(plr.Name)

    if Data then
        plr.Leaderstats.Dollars.Value = Data.Dollars
        plr.Leaderstats.Multi.Value = Data.Multi
        plr.Leaderstats.MustHos.Value = Data.MustHos
    else
        if not plr:FindFirstChild("Inventory") then
            local Inventory = Instance.new("Folder", plr)
            Inventory.Name = "Inventory"
        end

        if not plr:FindFirstChild("Leaderstats") then
            local Leaderstats = Instance.new("Folder", plr)
            Leaderstats.Name = "Leaderstats"

            local Dollars = Instance.new("NumberValue", Leaderstats)
            Dollars.Name = "Dollars"
            Dollars.Value = 5000

            local Multi = Instance.new("NumberValue", Leaderstats)
            Multi.Name = "Multi"
            Multi.Value = 1

            local MustHos = Instance.new("StringValue", Leaderstats)
            MustHos.Name = "MustHos"
            MustHos.Value = "None"
        end
    end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

1 Like

YES THANK YOU BRO it didn’t work at first but that is because you didn’t make the stuff if the player had data

1 Like

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