Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Using DataStore2 To set default value?

Okay I tired in many ways to make a default value and failed. Currently I deleted the part of the default ( most of it only the vars are available ) Tha's my code

local Rep = game:GetService("ReplicatedStorage")
local DataStore2 = require(Rep:WaitForChild("MainModule"))

DataStore2.Combine("DataStoreOPM", "Level", "Points", "Exp", "MaxExp", "Strength", "Health", "Agility", "Money")

-- Defaults
local DefaultLevel  = 1

local DefaultMaxExp = 50
local DefaultExp = 0
local DefaultPoints = 0

local DefaultStrength = 1 
local DefaultHealth = 1
local DefaultAgility = 1

local DefaultMoney = 200

game.Players.PlayerAdded:Connect(function(Plr)
	
	-- Store the values
	
	local LevelStore = DataStore2("Level",Plr)
	local ExpStore = DataStore2("Exp",Plr)
	local MaxExpStore = DataStore2("MaxExp", Plr)
	local StorePoints = DataStore2("Points", Plr)
	
	local StrengthStore = DataStore2("Strength",Plr)
	local HealthStore = DataStore2("Health",Plr)
	local AgilityStore = DataStore2("Agility",Plr)
	local MoneyStore = DataStore2("Money",Plr)
	
	-- Leaderstats
	local Data = Instance.new("Folder", Plr)
	Data.Name = "Data"
	
	local Level = Instance.new("IntValue", Data)
	Level.Name = "Level"
	
	local Exp = Instance.new("IntValue", Data)
	Exp.Name = "Exp"
	
	local MaxExp = Instance.new("IntValue", Data)
	MaxExp.Name = "MaxExp"
	MaxExp.Value = (Plr.Data.Level.Value + 1 ) * 1.25 *( 50)
	
	local Points = Instance.new("IntValue", Data)
	Points.Name = "Points"
	
	local Strength = Instance.new("IntValue", Data)
	Strength.Name = "Strength"
	
	local Health = Instance.new("IntValue", Data)
	Health.Name = "Health"
	
	local Agility = Instance.new("IntValue", Data)
	Agility.Name = "Agility"
	
	local Money = Instance.new("IntValue", Data)
	Money.Name = "Money"
	
	
	
	--  Update Functions
	
	local function UpdateLevel(GetData)
		Level.Value = LevelStore:Get(GetData)
	end
	
	local function UpdateExp(GetData)
		Exp.Value = ExpStore:Get(GetData)
	end
	
	local function UpdateMaxExp(GetData)
		MaxExp.Value = MaxExpStore:Get(GetData)
	end
	
	local function UpdatePoints(GetData)
		Points.Value = StorePoints:Get(GetData)
	end
	
	local function UpdateStrength(GetData)
		Strength.Value = StrengthStore:Get(GetData)
	end
	
	local function UpdateHealth(GetData)
		Health.Value = HealthStore:Get(GetData)
	end
	
	local function AgilityUpdate(GetData)
		Agility.Value = AgilityStore:Get(GetData)
	end
	
	local function MoneyUpdate(GetData)
		Money.Value = MoneyStore:Get(GetData)
	end
	
	-- Setting the functions here 
	UpdateExp()
	UpdateHealth()
	UpdateLevel()
	UpdateMaxExp()
	UpdatePoints()
	UpdateStrength()
	AgilityUpdate()
	MoneyUpdate()
	
	-- If the data update then save it
	
	ExpStore:OnUpdate(UpdateExp)
	HealthStore:OnUpdate(UpdateHealth)
	LevelStore:OnUpdate(UpdateLevel)
	StorePoints:OnUpdate(UpdatePoints)
	StrengthStore:OnUpdate(UpdateStrength)
	MoneyStore:OnUpdate(MoneyUpdate)
	AgilityStore:OnUpdate(AgilityUpdate)
	MaxExpStore:OnUpdate(UpdateMaxExp)
	
end)

I tried something like

	local function UpdateLevel(GetData)
		Level.Value = LevelStore:Get(GetData, DefaultLevel)
	end

but it won't work idk why, Thanks also!

2 answers

Try doing this instead:

function UpdateLevel(GetData)
	Level.Value = LevelStore:Get(DefaultLevel)
end

This will automatically get the data of the player plus the default value will be considered the default, as long as LevelStore is the datastore that is defined by:

LevelStore = DataStore2("LevelStore", player) -- player must be a player instance

You don't need anything else. Hope this helps!

2 comments

0

I have player as Plr.

0

I sent down here the new version and its not working, pls check.

I made this its still not working

local Rep = game:GetService("ReplicatedStorage")
local DataStore2 = require(Rep:WaitForChild("MainModule"))

DataStore2.Combine("DataStoreOPM", "Level", "Points", "Exp", "MaxExp", "Strength", "Health", "Agility", "Money")

-- Defaults
local DefaultLevel  = 1

local DefaultMaxExp = 50
local DefaultExp = 0
local DefaultPoints = 0

local DefaultStrength = 1 
local DefaultHealth = 1
local DefaultAgility = 1

local DefaultMoney = 200

game.Players.PlayerAdded:Connect(function(player)
	
	-- Store the values
	
	local LevelStore = DataStore2("Level",player)
	local ExpStore = DataStore2("Exp",player)
	local MaxExpStore = DataStore2("MaxExp", player)
	local StorePoints = DataStore2("Points", player)
	
	local StrengthStore = DataStore2("Strength",player)
	local HealthStore = DataStore2("Health",player)
	local AgilityStore = DataStore2("Agility",player)
	local MoneyStore = DataStore2("Money",player)
	
	-- Leaderstats
	local Data = Instance.new("Folder", player)
	Data.Name = "Data"
	
	local Level = Instance.new("IntValue", Data)
	Level.Name = "Level"
	
	local Exp = Instance.new("IntValue", Data)
	Exp.Name = "Exp"
	
	local MaxExp = Instance.new("IntValue", Data)
	MaxExp.Name = "MaxExp"
	MaxExp.Value = (player.Data.Level.Value + 1 ) * 1.25 *( 50)
	
	local Points = Instance.new("IntValue", Data)
	Points.Name = "Points"
	
	local Strength = Instance.new("IntValue", Data)
	Strength.Name = "Strength"
	
	local Health = Instance.new("IntValue", Data)
	Health.Name = "Health"
	
	local Agility = Instance.new("IntValue", Data)
	Agility.Name = "Agility"
	
	local Money = Instance.new("IntValue", Data)
	Money.Name = "Money"
	
		print("OnGoing")
	if player.Data.Level.Value < DefaultLevel then
		print("Test")
			LevelStore:Get(DefaultLevel)
			print("Yes")
			wait(2)
		end
	
	
	--  Update Functions
	
	local function UpdateLevel(GetData)
		Level.Value = LevelStore:Get(DefaultLevel)
	end
	
	local function UpdateExp(GetData)
		Exp.Value = ExpStore:Get(GetData)
	end
	
	local function UpdateMaxExp(GetData)
		MaxExp.Value = MaxExpStore:Get(GetData)
	end
	
	local function UpdatePoints(GetData)
		Points.Value = StorePoints:Get(GetData)
	end
	
	local function UpdateStrength(GetData)
		Strength.Value = StrengthStore:Get(GetData)
	end
	
	local function UpdateHealth(GetData)
		Health.Value = HealthStore:Get(GetData)
	end
	
	local function AgilityUpdate(GetData)
		Agility.Value = AgilityStore:Get(GetData)
	end
	
	local function MoneyUpdate(GetData)
		Money.Value = MoneyStore:Get(GetData)
	end
	
	-- Setting the functions here 
	UpdateExp()
	UpdateHealth()
	UpdateLevel()
	UpdateMaxExp()
	UpdatePoints()
	UpdateStrength()
	AgilityUpdate()
	MoneyUpdate()
	
	-- If the data update then save it
	
	ExpStore:OnUpdate(UpdateExp)
	HealthStore:OnUpdate(UpdateHealth)
	LevelStore:OnUpdate(UpdateLevel)
	StorePoints:OnUpdate(UpdatePoints)
	StrengthStore:OnUpdate(UpdateStrength)
	MoneyStore:OnUpdate(MoneyUpdate)
	AgilityStore:OnUpdate(AgilityUpdate)
	MaxExpStore:OnUpdate(UpdateMaxExp)
	
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.