the whole datastore script:
local datatemplate = require(script["Data-Template"])
local dataservice = game:GetService("DataStoreService")
local function loaddata(plr,folder)
local datas = {}
local GetDataStore = dataservice:GetDataStore("cooldata1//"..plr.UserId):GetAsync(plr.UserId)
for i,v in pairs(datatemplate["regular data"]) do
local valuecreated
if typeof(v) == "number" then
valuecreated = Instance.new("NumberValue",folder)
valuecreated.Name = i
elseif typeof(v) == "string" then
valuecreated = Instance.new("StringValue",folder)
valuecreated.Name = i
end
if GetDataStore then
local getvalue = GetDataStore[i]
if getvalue then
valuecreated.Value = getvalue
datas[i] = getvalue
else
valuecreated.Value = v
datas[i] = v
end
end
task.wait(.5)
end
task.wait()
end
local function loadInventory(plr,folder)
local Data,GetData = dataservice:GetDataStore("coolinvdata1//"..plr.UserId)
local ss = game:GetService("ServerStorage")
local tools = ss:WaitForChild("tools")
local success,err = pcall(function()
GetData = Data:GetAsync(plr.UserId)
end)
if err then
plr:Kick("error loading data, please try again.")
return
end
if GetData ~= nil then
for i,v in pairs(GetData) do
if tools:FindFirstChild(i) and v == true then
local clone,clone2 = tools:WaitForChild(i):Clone(),tools:WaitForChild(i):Clone()
clone.Parent = plr.Backpack
clone2.Parent = folder
end
end
else
for i,v in pairs(datatemplate.inventory) do
if tools:FindFirstChild(i) and v == true then
local clone,clone2 = tools:WaitForChild(i):Clone(),tools:WaitForChild(i):Clone()
clone.Parent = plr.Backpack
clone2.Parent = folder
end
end
end
end
local function loadLevelInfo(plr)
local datas = {}
local GetDataStore = dataservice:GetDataStore("coolleveldata1//"..plr.UserId):GetAsync(plr.UserId)
for i,v in pairs(datatemplate.levelinfo) do
local valuecreated
if typeof(v) == "number" then
valuecreated = Instance.new("NumberValue",plr)
valuecreated.Name = i
elseif typeof(v) == "string" then
valuecreated = Instance.new("StringValue",plr)
valuecreated.Name = i
end
if GetDataStore then
local getvalue = GetDataStore[i]
if getvalue then
valuecreated.Value = getvalue
datas[i] = getvalue
else
valuecreated.Value = v
datas[i] = v
end
end
task.wait(.5)
end
task.wait()
end
local function lvlupEffect(char)
local effect = game:GetService("ServerStorage"):WaitForChild("Level Up"):Clone()
effect.Parent = workspace
effect.CFrame = char.HumanoidRootPart.CFrame
for i,v in pairs(effect:GetDescendants()) do
if v:IsA("ParticleEmitter") then
v:Emit(v.Name)
end
end
game:GetService("Debris"):AddItem(effect,5)
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
for i,v in pairs(workspace.Enemies:GetChildren()) do
local attacker = v:FindFirstChild("attacker")
if attacker and attacker.Value == plr.Name then
attacker.Value = ""
end
end
local inventoryfolder = Instance.new("Folder",game:GetService("ServerStorage"))
inventoryfolder.Name = plr.Name
loadInventory(plr,inventoryfolder)
coroutine.resume(coroutine.create(function()
repeat
task.wait()
until plr:FindFirstChild("hp")
task.wait(0.05)
char.Humanoid.MaxHealth = plr:WaitForChild("hp").Value
char.Humanoid.Health = plr:WaitForChild("hp").Value
plr:WaitForChild("hp").Changed:Connect(function()
char.Humanoid.MaxHealth = plr:WaitForChild("hp").Value
end)
end))
end)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
loaddata(plr,leaderstats)
loadLevelInfo(plr)
task.wait(2)
coroutine.resume(coroutine.create(function()
game:GetService("RunService").Heartbeat:Connect(function()
local lvl = leaderstats:FindFirstChild("levels")
local progresslvl = plr:FindFirstChild("levelprogress")
local maxlvl = plr:FindFirstChild("levelmax")
local hp = plr:WaitForChild("hp")
if progresslvl.Value >= maxlvl.Value then
lvlupEffect(plr.Character)
lvl.Value += 1
progresslvl.Value = 0
maxlvl.Value *= 1.2
hp.Value += 1.2
local round,roundhp = math.floor(maxlvl.Value),math.floor(hp.Value)
hp.Value = roundhp
maxlvl.Value = round
end
if plr:FindFirstChild("rest") then
progresslvl.Value += plr:FindFirstChild("rest").Value
plr:WaitForChild("rest"):Destroy()
end
end)
end))
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i,v in pairs(workspace.Enemies:GetChildren()) do
local attacker = v:FindFirstChild("attacker")
if attacker and attacker.Value == plr.Name then
attacker.Value = ""
end
end
local DataTable,Inventory,LVLInfo = {},{},{}
local leaderstats,inventoryfolder = plr:FindFirstChild("leaderstats"),game:GetService("ServerStorage"):FindFirstChild(plr.Name)
local GetData,GetData2,GetData3 = dataservice:GetDataStore("cooldata1//"..plr.UserId),dataservice:GetDataStore("coolinvdata1//"..plr.UserId),dataservice:GetDataStore("coolleveldata1//"..plr.UserId)
if leaderstats and inventoryfolder then
for i,v in pairs(leaderstats:GetChildren()) do
DataTable[v.Name] = v.Value
end
for i,v in pairs(inventoryfolder:GetChildren()) do
Inventory[v.Name] = true
end
for i,v in pairs(plr:GetChildren()) do
if v:IsA("NumberValue") then
LVLInfo[v.Name] = v.Value
end
end
GetData:SetAsync(plr.UserId,DataTable)
GetData2:SetAsync(plr.UserId,Inventory)
GetData3:SetAsync(plr.UserId,LVLInfo)
else
warn("data didn't load")
end
end)
the template:
local data = {
["regular data"] = {
levels = 1,
gold = 1,
},
inventory = {
["wood sword"] = true
},
["levelinfo"] = {
levelprogress = 0,
levelmax = 100,
hp = 50,
}
}
return data
for odd reason the script doesnt use the template, either a really simple fix or im doing something completely wrong.