Error with DataStore2

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!
    Im trying to use DataStore2 to save my Data (Wins, Level and Exp).

  2. What is the issue? Include screenshots / videos if possible!
    When I go into Play, I get this error: ServerScriptService.DataStore2:479: DataStore2() API call expected {string dataStoreName, Instance player}, got {table, Instance}
    Not sure where it’s getting a table instead of a string in the script.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried finding solutions, but didn’t find any.

Script inside of ServerScriptService

local players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService.DataStore2)

DataStore2:Combine("Data", "wins")
DataStore2:Combine("Data", "level")
DataStore2:Combine("Data", "exp")

players.PlayerAdded:Connect(function(player)
	local winsStore = DataStore2("wins", player)
	local levelStore = DataStore2("level", player)
	local expStore = DataStore2("exp", player)

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	winsStore:Get(0)
	Wins.Parent = leaderstats

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	levelStore:Get(1)
	Level.Parent = leaderstats

	local statsfolder = Instance.new("Folder")
	statsfolder.Name = "Stats"
	statsfolder.Parent = player

	local Exp = Instance.new("IntValue")
	Exp.Name = "Exp"
	expStore:Get(0)
	Exp.Parent = statsfolder


	winsStore:OnUpdate(function(newValue)
		Wins.Value = newValue
	end)

	levelStore:OnUpdate(function(newValue)
		Level.Value = newValue
	end)

	expStore:OnUpdate(function(newValue)
		Exp.Value = newValue
	end)

	Exp.Changed:Connect(function()
		if Exp.Value >= Level.Value * 50 then
			levelStore:Increment(1)
			expStore:Set(Exp.Value - Level.Value * 50)
		end
	end)

end)

This is my first time using DataStore2, so it might just me being dumb lol

I see what you are doing wrong. DataStore2 function expects a string for the data store name, but you are passing a table instead.

local players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService.DataStore2)

local winsDataStore = "wins"
local levelDataStore = "level"
local expDataStore = "exp"

players.PlayerAdded:Connect(function(player)
    local winsStore = DataStore2(winsDataStore, player)
    local levelStore = DataStore2(levelDataStore, player)
    local expStore = DataStore2(expDataStore, player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Wins = Instance.new("IntValue")
    Wins.Name = "Wins"
    Wins.Value = winsStore:Get(0) -- Retrieve the value and set it initially
    Wins.Parent = leaderstats

    local Level = Instance.new("IntValue")
    Level.Name = "Level"
    Level.Value = levelStore:Get(1) -- Retrieve the value and set it initially
    Level.Parent = leaderstats

    local statsfolder = Instance.new("Folder")
    statsfolder.Name = "Stats"
    statsfolder.Parent = player

    local Exp = Instance.new("IntValue")
    Exp.Name = "Exp"
    Exp.Value = expStore:Get(0) -- Retrieve the value and set it initially
    Exp.Parent = statsfolder

    winsStore:OnUpdate(function(newValue)
        Wins.Value = newValue
    end)

    levelStore:OnUpdate(function(newValue)
        Level.Value = newValue
    end)

    expStore:OnUpdate(function(newValue)
        Exp.Value = newValue
    end)

    Exp.Changed:Connect(function()
        if Exp.Value >= Level.Value * 50 then
            levelStore:Increment(1)
            expStore:Set(Exp.Value - Level.Value * 50)
        end
    end)
end)

Happy to help! Feel free to reach out to me if you have any further questions.

1 Like

I do have some questions. Why should I use the :Combine Function? In the fixed script you sent me, you didn’t use it.

The choice to use :Combine() depends on the structure of your data and how you want to organize it in your data store.

1 Like

If you really want to use :Combine(), I can change it for you.

1 Like

No need to, I understand how to use it now. Thank you for the Info tho!

1 Like

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