DataStore2 folders (and items within folders) not being created upon game start

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

  1. What do you want to achieve? I want to use DataStore2 to save Stats

  2. What is the issue? The script is not creating a folder for Stats (or the player inventory)

  3. What solutions have you tried so far? https://www.youtube.com/watch?v=hmRBvZD1pRw used this tutorial to help make the script

Here is the script
– Var
local DataStore2 = require(game.ServerScriptService.MainModule)
local MainKey = “MainKey”
DataStore2.Combine(MainKey,“Inv”,“Stats”)

– Tables
local function SetDataTable()
local UserData = {
Stats = {
[“Tix”] = 0,
[“KOs”] = 0,
[“WOs”] = 0,

	},
	
	Inv = {
		["Test"] = false,
		
		},
	
}
return UserData
end

game.Players.PlayerAdded:Connect(function(plr)
local UserData = DataStore2(MainKey,plr):Get(SetDataTable())

local Stats = Instance.new("Folder")
Stats.Name = "Stats"
local invfolder = Instance.new("Folder")
invfolder.Name = "Inv"

local Tix = Instance.new("IntValue")
Tix.name = "Tix"
local KOs = Instance.new("IntValue")
KOs.name = "KOs"
local WOs = Instance.new("IntValue")
WOs.name = "WOs"

local Waited5Seconds = Instance.new("BoolValue")
Waited5Seconds.name = "WaitedFor5Seconds"
local CLickedTheButton = Instance.new("BoolValue")
CLickedTheButton.Name = "ClickedTheButton"

local StatsData = DataStore2("Stats",plr)
local InvData = DataStore2("Inv",plr)

local function UpdateStats(UpdatedValue)
	Tix.Value = StatsData:Get(UpdatedValue).Tix
	KOs.Value = StatsData:Get(UpdatedValue).KOs
	WOs.Value = StatsData:Get(UpdatedValue).WOs
	end

local function UpdateAch(UpdatedValue)
	Waited5Seconds.Value = InvData:Get(UpdatedValue).Waited5Seconds
	CLickedTheButton.Value = InvData:Get(UpdatedValue).ClickedTheButton
end

UpdateStats(UserData.Stats)
UpdateAch(UserData.Inv)

StatsData.OnUpdate(UpdateStats)
InvData.OnUpdate(UpdateAch)

Stats.Parent = plr
invfolder.Parent = plr
Tix.Parent = Stats
WOs.Parent = Stats
KOs.Parent = Stats
Waited5Seconds.Parent =  invfolder
CLickedTheButton.Parent = invfolder
while true do
wait(60)
   end
end)

Here’s a sort of fix,

Basically you have an error with this line here:

local UserData = DataStore2(MainKey,plr):Get(SetDataTable())

I don’t think you really need a function for this, I think you can just set a variable and be sort of fine(?) - I’m not really that sure, I’m not this familiar with DataStore2’s API. However, the script I posted before does put your folders and items in the character. The issue was that the local UserData link I linked above. I would just recheck how you’re interacting with the API and make sure your formatting is good.

Also, just as a word of caution I would just be careful about having multiple variables named UserData even though they are both in different contexts both so that you don’t get confused or you don’t make an accidental syntax/formatting mistake and then the script starts erroring because it’s calling the wrong value.

Hope this was helpful

"Fixed" script that loads the folders and items. Just fix that one line and you're set!
local DataStore2 = require(game.ServerScriptService.MainModule)
local MainKey = "MainKey"
DataStore2.Combine(MainKey,"Inv","Stats")

local function SetDataTable()
	local UserData = {
	Stats = {
	["Tix"] = 0,
	["KOs"] = 0,
	["WOs"] = 0,

	},
	
	Inv = {
		["Test"] = false,
		
		},
	
	}
return UserData
end

game.Players.PlayerAdded:Connect(function(plr)
	local UserData = DataStore2(MainKey,plr):Get(SetDataTable()) --*ERROR HERE* Error with this line, go recheck you're doing this right

	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	local invfolder = Instance.new("Folder")
	invfolder.Name = "Inv"

	local Tix = Instance.new("IntValue")
	Tix.Name = "Tix" --*ERROR HERE* (fixed tho) You had Tix.name rather than Tix.Name
	local KOs = Instance.new("IntValue")
	KOs.Name = "KOs" --*ERROR HERE* (fixed tho)You had KOs.name rather than KOs.Name
	local WOs = Instance.new("IntValue")
	WOs.Name = "WOs" --*ERROR HERE* (fixed tho)You had WOs.name rather than WOs.Name

	local Waited5Seconds = Instance.new("BoolValue")
	Waited5Seconds.Name = "WaitedFor5Seconds" --*ERROR HERE* (fixed tho)You had Waited5Seconds.name rather than Waited5Seconds.Name
	local CLickedTheButton = Instance.new("BoolValue")
	CLickedTheButton.Name = "ClickedTheButton"

	local StatsData = DataStore2("Stats",plr)
	local InvData = DataStore2("Inv",plr)
	
	Stats.Parent = plr
	invfolder.Parent = plr
	Tix.Parent = Stats
	WOs.Parent = Stats
	KOs.Parent = Stats
	Waited5Seconds.Parent =  invfolder
	CLickedTheButton.Parent = invfolder

	local function UpdateStats(UpdatedValue)
		Tix.Value = StatsData:Get(UpdatedValue).Tix
		KOs.Value = StatsData:Get(UpdatedValue).KOs
		WOs.Value = StatsData:Get(UpdatedValue).WOs
	end

	local function UpdateAch(UpdatedValue)
		Waited5Seconds.Value = InvData:Get(UpdatedValue).Waited5Seconds
		CLickedTheButton.Value = InvData:Get(UpdatedValue).ClickedTheButton
	end

	UpdateStats(UserData.Stats)
	UpdateAch(UserData.Inv)

	StatsData.OnUpdate(UpdateStats)
	InvData.OnUpdate(UpdateAch)

	Stats.Parent = plr
	invfolder.Parent = plr
	Tix.Parent = Stats
	WOs.Parent = Stats
	KOs.Parent = Stats
	Waited5Seconds.Parent =  invfolder
	CLickedTheButton.Parent = invfolder
	
	while true do
		wait(60)
   end
end)

Eyo, thanks for watching my youtube tutorial :+1:.

Though, there are some problems I’d like to adress with the video (I’m planning to change and update it)

Try reading through this tutorial to see if it’ll fix your problem:

1 Like

Thanks for replying! it seems I have found the error on my script the video helped me make

" [10:36:54.803 - name is not a valid member of IntValue]"

im new to scripting so im not sure what this means and not sure how to fix it.

Using the guide you linked gave me this error
" [10:44:10.257 - ServerScriptService.DataMain:52: ‘end’ expected (to close ‘function’ at line 21) near ‘’]"

Script:

local DataStore2 = require(game:GetService(“ServerScriptService”).MainModule) – Require the DataStore Module using the name
local MainKey = “oldBLOX”

DataStore2.Combine(MainKey, “Stats”, “Inv”)

local function CreateDataTable()
local PlayerData = {
Stats = {
[“Tix”] = 0;
[“WOs”] = 0;
[“KOs”] = 0;
};
Inv = {
[“Cap1test”] = false;
[“Cap2Test”] = false;
};
}
return PlayerData
end

game:GetService(“Players”).PlayerAdded:Connect(function(plr)
local PlayerData = DataStore2(MainKey, plr):Get(CreateDataTable())

local Stats = Instance.new(“Folder”)
Stats.Name = “Stats”

local Stat = Instance.new(“IntValue”)
Stats.Name = “Stage”

local InvData = DataStore2(“Inv”, plr)
local StatsData = DataStore2(“Stats”, plr)

local function UpdateAllStats(UpdatedStats)
Stat.Value = DataStore2(Key, plr):Get(UpdatedStats)[“Stats”][“Inv”] – Sets value you’ve made to correspond with data table
end

UpdateAllStats(PlayerData)
DataStore2(Key, plr):OnUpdate(UpdateAllStats)

LS.Parent = plr
Stat.Parent = LS

Local.Inv = InvData:Get()
Inv.Cap2Test = true
InvData:Set(Inv)

RemoteEvent.OnServerEvent:Connect(function(ThePlayer)
local Inv = DataStore2(“Inv”, ThePlayer):Get()
Inv.Cap2Test = true
DataStore2(“Inv”, ThePlayer):Set(Inv )
end)

EDIT: You forgot to add one end at the end of the code to close your PlayerAdded Connection.
Try this:

local DataStore2 = require(game:GetService("ServerScriptService").MainModule) -- Require the DataStore Module using the name
local MainKey = "oldBLOX"

DataStore2.Combine(MainKey, "Stats", "Inv")

local function CreateDataTable()
	local PlayerData = {
		Stats = {
			["Tix"] = 0;
			["WOs"] = 0;
			["KOs"] = 0;
		};
		Inv = {
			["Cap1test"] = false;
			["Cap2Test"] = false;
		};
	}
	return PlayerData
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
    local PlayerData = DataStore2(MainKey, plr):Get(CreateDataTable())
    
    local Stats = Instance.new("Folder")
    Stats.Name = "Stats"
    
    local Stat = Instance.new("IntValue")
    Stats.Name = "Stage"
    
    local InvData = DataStore2("Inv", plr)
    local StatsData = DataStore2("Stats", plr)
    
    local function UpdateAllStats(UpdatedStats)
    	Stat.Value = DataStore2(Key, plr):Get(UpdatedStats)["Stats"]["Inv"] -- Sets value you’ve made to correspond with data table
    end
    
    UpdateAllStats(PlayerData)
    DataStore2(Key, plr):OnUpdate(UpdateAllStats)
    
    LS.Parent = plr
    Stat.Parent = LS
    
    Local.Inv = InvData:Get()
    Inv.Cap2Test = true
    InvData:Set(Inv)
end)

RemoteEvent.OnServerEvent:Connect(function(ThePlayer)
	local Inv = DataStore2("Inv", ThePlayer):Get()
	Inv.Cap2Test = true
	DataStore2("Inv", ThePlayer):Set(Inv)
end)
1 Like

sorry for being off-topic but how do you put the script in the nice box like that?

1 Like

use three backticks:

```
--YourCodeInHere
```