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

Why can't this leaderstats script work (it wont make leaderstats folder) ?

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("Brick")
local ds2 = datastore:GetDataStore("Wood")
local ds3 = datastore:GetDataStore("Level")
local ds4 = datastore:GetDataStore("XP")
local ds5 = datastore:GetDataStore("MaxXP")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local gems = Instance.new("IntValue", folder)
 gems.Name = "Wood"
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Brick"
 local lvl = Instance.new("IntValue", folder)
 lvl.Name = "Level"
 local xp = Instance.new("IntValue", folder)
 xp.Name = "XP"
 local max = Instance.new("IntValue", folder)
 max.Name = "MaxXP"
if max.Value == 0 then
	max.Value = 100
end
if lvl.Value == 0 then
	lvl.Value = 1
end
 
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)

 lvl.Value = ds3:GetAsync(plr.UserId) or 0
 ds3:SetAsync(plr.UserId, lvl.Value)

 xp.Value = ds4:GetAsync(plr.UserId) or 0
 ds3:SetAsync(plr.UserId, xp.Value)

 max.Value = ds5:GetAsync(plr.UserId) or 0
 ds5:SetAsync(plr.UserId, max.Value)

 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 
 cash.Changed:connect(function()
  ds2:SetAsync(plr.UserId, cash.Value)
 end)

 lvl.Changed:connect(function()
  ds3:SetAsync(plr.UserId, lvl.Value)
 end)

 xp.Changed:connect(function()
  ds4:SetAsync(plr.UserId, xp.Value)
 end)

 max.Changed:connect(function()
  ds5:SetAsync(plr.UserId, max.Value)
 end)

end)

wood and brick is cash and gems cos

3 comments

0

Errors?

0

no

0

replace folder with boolvalue

2 answers

✓ Accepted answer

You need to make sure Studio API Service is enabled to do this open up the game then press the Home tab at the top of studio and navigate to Game Settings once you have clicked on Game Settings press the Options tab on the left hand side and switch "Enable studio access to api services" to On

Also make sure this is a script not a local script and this script is Located in ServerScriptService

2 comments

0

I did not publish game

0

:facepalm: publish it then

In addition to what prestore said, you could also have a table containing all of your player's data, rather than a new store for each. You're going to get throttled using the Changed event. DataStores have a limit to how much you can save in a certain amount of time. You may consider saving data when a player leaves and a simple backup every x seconds. The below code is basic and does not cover pitfalls and caching, but you can get another idea for your next iterations as you continue.

local datastore = game:GetService("DataStoreService")
local PlayerData = datastore:GetDataStore("PlayerData");

local function key(player)
	-- returns 'azarth_#userid'
	return ('%s_%d'):format(player.Name, player.UserId)
end

local dummy_data = {
	['Wood'] = 0;
	['Brick'] = 0;
	['Level'] = 1;
	['XP'] = 0;
	['gems'] = 0;
	['MaxXP'] = 100;
}

local function load_data(player)
	-- return loaded data or dummy_data if we haven't played before
	return PlayerData:GetAsync( key(player) ) or dummy_data
end

local function create_leaderstats(player, data)
	local leaderstats_obj = Instance.new("StringValue")
	leaderstats_obj.Name = 'leaderstats'
	-- loop through our datastore table to create leaderstats
	for name,value in pairs(data) do 
		local stat = Instance.new("NumberValue")
		stat.Name = name;
		stat.Value = value
		stat.Parent = leaderstats_obj
	end
	leaderstats_obj.Parent = player
	return leaderstats_obj
end

local function save_data(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then  warn( ' no leaderstats ' ) return end
	local success, msg = pcall(function()
		PlayerData:UpdateAsync( key(player), function(old_data)
			local newdata = {}
			for i,v in pairs(leaderstats:GetChildren()) do 
				if old_data then 
					-- add to old data, instead of accidently resetting data
					local value = old_data[v.Name]
					-- constrain to certain names
					if value then 
						local difference =  ( v.Value - value ) 
						-- the ammount we made since the last save
						-- add to old data
						newdata[v.Name] = value + difference
					end
				else
					-- save current new data 
					local value = dummy_data[v.Name]
					if value then 
						newdata[v.Name] = v.Value
					end
				end
			end
			return newdata
		end)
	end)
	if success then 
		print( (' Data for %s saved successfully'):format(player.Name) ) 
	else
		print ( "You should cache the player next time, data did not save" )
	end
end

-- Uppercase 'C' in connect, lowercase is deprecated
game.Players.PlayerAdded:Connect(function(player)
	local data = load_data(player)
	create_leaderstats(player, data )
end)

game.Players.PlayerRemoving:Connect(function(player)
	-- save when leaving
	save_data(player)
end)

-- basic backup
while wait(120) do 
	for i,v in pairs(game.Players:GetPlayers()) do 
		save_data(v)
	end
end

1 comment

0

^Agreed

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