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

My data store script has an error and I don't know why?

It says Workspace.Stat:61 attempt to index local 'GetSaved' (a number value) what is wrong??

local DataStorage = game:GetService("DataStoreService"):GetDataStore("OPUTTUdata")

game.Players.PlayerAdded:connect(function(plr)
	local specialkey = "ID-"..plr.userId
	local PlayerData = Instance.new("Folder", game:GetService("ServerStorage"))
	PlayerData.Name = plr.Name
	local Level = Instance.new("IntValue", PlayerData)
	Level.Name = "Level"
	Level.Value = 1
	local LevelXP = Instance.new("IntValue", PlayerData)
	LevelXP.Name = "LevelXP"
	LevelXP.Value = 1
	local Melee = Instance.new("IntValue", PlayerData)
	Melee.Name = "Melee"
	Melee.Value = 1
	local MeleeXP = Instance.new("IntValue", PlayerData)
	MeleeXP.Name = "MeleeXP"
	MeleeXP.Value = 1
	local Sword = Instance.new("IntValue", PlayerData)
	Sword.Name = "Sword"
	Sword.Value = 1
	local SwordXP = Instance.new("IntValue", PlayerData)
	SwordXP.Name = "SwordXP"
	SwordXP.Value = 1
	local Sniper = Instance.new("IntValue", PlayerData)
	Sniper.Name = "Sniper"
	Sniper.Value = 1
	local SniperXP = Instance.new("IntValue", PlayerData)
	SniperXP.Name = "SniperXP"
	SniperXP.Value = 1
	local Armanant = Instance.new("IntValue", PlayerData)
	Armanant.Name = "Armanant"
	Armanant.Value = 1
	local ArmanantXP = Instance.new("IntValue", PlayerData)
	ArmanantXP.Name = "ArmanantXP"
	ArmanantXP.Value = 1
	local Observation = Instance.new("IntValue", PlayerData)
	Observation.Name = "Observation"
	Observation.Value = 1
	local ObservationXP = Instance.new("IntValue", PlayerData)
	ObservationXP.Name = "ObservationXP"
	ObservationXP.Value = 1
	local Conqueror = Instance.new("IntValue", PlayerData)
	Conqueror.Name = "Conqueror"
	Conqueror.Value = 0
	local ConquerorXP = Instance.new("IntValue", PlayerData)
	ConquerorXP.Name = "ConquerorXP"
	ConquerorXP.Value = 1
	local Beri = Instance.new("IntValue", PlayerData)
	Beri.Name = "Beri"
	Beri.Value = 0
	
	
	
	--Get Async gets infomation and if there is it will save it, hence Get Saved [1] to [15]
	--else means that if there is no infomation then we're going to save whats inside Numbers For Saving
	--SetAsync sets the data and GetAsync loads it
	
	local GetSaved = DataStorage:GetAsync(specialkey)
	if GetSaved then
		Level.Value = GetSaved[1]
		LevelXP.Value = GetSaved[2]
		Melee.Value = GetSaved[3]
		MeleeXP.Value = GetSaved[4]
		Sword.Value = GetSaved[5]
		SwordXP.Value = GetSaved[6]
		Sniper.Value = GetSaved[7]
		SniperXP.Value = GetSaved[8]
		Armanant.Value = GetSaved[9]
		ArmanantXP.Value = GetSaved[10]
		Observation.Value = GetSaved[11]
		ObservationXP.Value = GetSaved[12]
		Conqueror.Value = GetSaved[13]
		ConquerorXP.Value = GetSaved[14]
		Beri.Value = GetSaved[15]
	else
		local NumbersForSaving = {Level.Value, LevelXP.Value, Melee.Value, MeleeXP.Value, Sword.Value, SwordXP.Value, Sniper.Value, SniperXP.Value, Armanant.Value, ArmanantXP.Value, Observation.Value, ObservationXP.Value, Conqueror.Value, ConquerorXP.Value, Beri.Value}
		DataStorage:SetAsync(specialkey, NumbersForSaving)
		
	end
		Level.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Level.Value)
	end)
	
	Melee.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Melee.Value)
	end)
	
	Sword.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Sword.Value)
	end)
	
	Sniper.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Sniper.Value)
	end)
	
	Armanant.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Armanant.Value)
	end)
	
	Observation.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Observation.Value)
	end)
	
	Conqueror.Changed:connect(function()
		wait(1)
		DataStorage:SetAsync(specialkey, Conqueror.Value)
	end)
end)





1 comment

0

In all of your Changed events, you are overwriting specialkey. You are storing one number and not a table.

1 answer

✓ Accepted answer

You should not be using a data store to save every small change, there are Request_limit as well as other limitations.

As you are reading this data as an array you must save this data as a table (Also must comply with data store limitations). You currently have it setup to only save one value (not a table of value) when one of the stats changes. This would wipe all existing data associated with the key.

Data stores are very complicated and must be managed correctly as there are a lot of potential problems e.g. network related, the server who is receiving the request is overloaded which would reject the request or other things to do with safely saving data and not overwriting it.

Back to the problem:-

local DataStorage = game:GetService("DataStoreService"):GetDataStore("OPUTTUdata")

-- we should load the player data when the join
game.Players.PlayerAdded:connect(function(plr)
	local data  = DataStorage:GetAsync('id_' .. tostring(plr.UserId))
	if data then
		-- do something with the data
		--e.g level.Value = data[2]
	else
		-- player does not have any data
	end
end)

-- we should save player data when the leave the game
game.Players.PlayerRemoving:connect(function (plr)
	local tmp = {plr.xp, plr.level}
	DataStorage:SetAsync('id_' .. tostring(plr.UserId), tmp) -- save the data in a table format
end

This is an example only, this will not manage any data store failures which would make the script error.

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

2 comments

0

What is "tostring" there for? does it convert it into string form?

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