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

Problems with datastore script not running after a certain point?

Problem: So this is my first time really making a datastore so I am unsure as I go with the scripting process. I was wondering what I am doing wrong/if I am doing it inefficiently because in line 56 it just says "stack end". It does not continue the code any further. Any tips/help?

code:

local store = game:GetService("DataStoreService"):GetDataStore("PlayerData")

game.Players.PlayerAdded:connect(function(plr)
	local fold = Instance.new("Folder")
	fold.Parent = plr
	fold.Name = "leaderstats1"
	fold.Parent = plr
	local xp = Instance.new("IntValue")
	xp.Name = "Experience"
	xp.Parent = fold
	local maxexp = Instance.new("IntValue")
	maxexp.Name = "MaxExp"
	maxexp.Parent = fold
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = fold
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = fold
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = fold
	local tries = Instance.new("IntValue")
	tries.Name = "Tries"
	tries.Parent = fold
	local votes = Instance.new("IntValue")
	votes.Name = "Votes"
	votes.Parent = fold
	local mana = Instance.new("IntValue")
	mana.Name = "Mana"
	mana.Parent = fold
	local maxmana = Instance.new("IntValue")
	maxmana.Name = "MaxMana"
	maxmana.Parent = fold
	local swordskill = Instance.new("StringValue")
	swordskill.Name = "SwordSkill"
	swordskill.Parent = fold
	local power = Instance.new("StringValue")
	power.Name = "Power"
	power.Parent = fold
	local third = Instance.new("StringValue")
	third.Name = "Third"
	third.Parent = fold
	local owner = Instance.new("BoolValue")
	owner.Name = "Owner"
	owner.Parent = fold
	local newplayer = Instance.new("BoolValue")
	newplayer.Name = "NewPlayer"
	newplayer.Parent = fold
	local category = Instance.new("StringValue")
	category.Name = "Category"
	category.Parent = fold
	print("Made the values")
	local key = "user-"..plr.Name
	print("Made user variable")
	local items = store:GetAsync(key)
	print("Preparing")
	if items then
		xp.Value = items[1]
		maxexp.Value = items[2]
		level.Value = items[3]
		cash.Value = items[4]
		gems.Value = items[5]
		tries.Value = items[6]
		votes.Value = items[7]
		mana.Value = items[8]
		maxmana.Value = items[9]
		swordskill.Value = items[10]
		power.Value = items[11]
		third.Value = items[12]
		owner.Value = items[13]
		newplayer.Value = items[14]
		category.Value = items[15]
		print("Loaded")
	else
		local values = {
		xp.Value,
		maxexp.Value,
		level.Value,
		cash.Value,
		gems.Value,
		tries.Value,
		votes.Value,
		mana.Value,
		maxmana.Value,
		swordskill.Value,
		power.Value,
		third.Value,
		owner.Value,
		newplayer.Value,
		category.Value,
	}
		store:SetAsync(key, values)
		print("Had to make new ones")
	end
	if level.Value == 1 or level.Value == 0 then
		if xp.Value == 1 or xp.Value == 0 then
			if swordskill.Value == "" then
				if power.Value == "" then
					if third.Value == "" then
						plr:WaitForChild("leaderstats1").tries.Value = plr:WaitForChild("leaderstats1").tries.Value + 10
						newplayer.Value = true
						if tries.Value > 400 then
							tries.Value = 400
							print("Was new player")
						end
					end
				end
			end
		end
	end
end)
game.Players.PlayerRemoving:connect(function(plr)
	local values = {
		plr.leaderstats1.xp.Value,
		plr.leaderstats1.maxexp.Value,
		plr.leaderstats1.level.Value,
		plr.leaderstats1.cash.Value,
		plr.leaderstats1.gems.Value,
		plr.leaderstats1.tries.Value,
		plr.leaderstats1.votes.Value,
		plr.leaderstats1.mana.Value,
		plr.leaderstats1.maxmana.Value,
		plr.leaderstats1.swordskill.Value,
		plr.leaderstats1.power.Value,
		plr.leaderstats1.third.Value,
		plr.leaderstats1.owner.Value,
		plr.leaderstats1.newplayer.Value,
		plr.leaderstats1.category.Value,
	}
	local key = "user-"..plr.Name
	store:SetAsync(key, values)
	print("Did it")
end)

3 comments

0

Why you have the players removing function in the player added function?

0

Because I put too may lines before the remove event so I thought that I ended the first event before putting the next one. Fixed it :P

0

This data will not be saving as you have not accessed the correct name, e.g you have xp when it should be Experience as that is the objects name.

1 answer

✓ Accepted answer

This is just some additional pointers that may help:-

local store = game:GetService("DataStoreService"):GetDataStore("PlayerData")

-- this should remove a lot of duplicate code
local function mkStat(insType, name, par)
	local tmp = Instance.new(insType, par)
	tmp.Name = name
	return tmp
end

local function saveData(key, data)
	store:SetAsync(key, data)
end

local function getData(key)
	return store:GetAsync(key)
end

-- it might be better to setup the stats here 
local stats = mkStat('Folder', 'leaderstats1')
mkStat('IntValue', 'Experience', stats)
mkStat('IntValue','MaxExp',stats)
mkStat('IntValue','Level',stats)
mkStat('IntValue','Cash',stats)
mkStat('IntValue','Gems',stats)
mkStat('IntValue','Tries',stats)
mkStat('IntValue','Votes',stats)
mkStat('IntValue','Mana',stats)
mkStat('IntValue','MaxMana',stats)
mkStat('StringValue','SwordSkill',stats)
mkStat('StringValue','Power',stats)
mkStat('StringValue','Third',stats)
mkStat('BoolValue','Owner',stats)
mkStat('BoolValue','NewPlayer',stats)
mkStat('StringValue','Category',stats)

game.Players.PlayerAdded:connect(function(plr)
	local clnStats = stats:Clone() -- we clone from our pre made stats
	local key = 'user-' .. plr.Name
	local data = getData(key)
	
	if data then
		-- instead of using an array we could use a key value pair
		for statName, val in pairs(data) do
			if clnStats[statName] then
				clnStats[statName].Value = val
			else
				print('Stat not found ' .. statName) 
			end
		end
	else
		local tmpTbl = {}
		for _, obj in pairs(stats:GetChildren()) do
			tmpTbl[obj.Name] = obj.Value
		end
		saveData(key, tmpTbl)
	end
	
	if clnStats.Level.Value == 1 or clnStats.Level.Value == 0 then
		if clnStats.Experience.Value == 1 or clnStats.Experience.Value == 0 then
			if clnStats.SwordSkill.Value == '' and clnStats.Power.Value == '' and clnStats.Third.Value == '' then
				clnStats.Tries.Value = clnStats.Tries.Value + 10
				if clnStats.Tries.Value > 400 then
					clnStats.Tries.Value = 400
					print('New Player')
				end
			end
		end
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local tmpTbl = {}
	for _, obj in pairs(plr.leaderstats1:GetChildren()) do
		tmpTbl[obj.Name] = obj.Value
	end
	saveData(key, tmpTbl)
end

The code above is just an example of a different method of saving data i.e. not using an array but a key value pair instead.

I hope this helps, please comment if you do not understand how / why this code works.

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