Help with Datastore Scipt

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    im making multiple datastores
  2. What is the issue? Include screenshots / videos if possible!
    it just doesnt work
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i dont have any help :sob:
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


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


local function onPlayerJoin(player)  -- Runs when players join
	
	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local kill = Instance.new("IntValue") --Sets up value for leaderstats
	kill.Name = "Kills"
	kill.Parent = leaderstats
	
	local cash = Instance.new("IntValue") --Sets up value for leaderstats
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local death = Instance.new("IntValue") --Sets up value for leaderstats
	death.Name = "Deaths"
	death.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			local tag = character.Humanoid:FindFirstChild("creator")
			if tag ~= nil then
				local player = tag.Value
				local bounty = 1
				local leaderstats = player:WaitForChild("leaderstats")
				leaderstats.Kills.Value += bounty
				leaderstats.Cash.Value += 100
			end
		end)
		player.CharacterAdded:Connect(function(char)

			local humanoid = char:FindFirstChild("Humanoid")

			humanoid.Died:Connect(function()
				local leaderstats = player:WaitForChild("leaderstats")
				leaderstats.Deaths.Value += 1
			end)
		end)
	end)
	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	if data then
		cash.Value = data["Cash"].Value
		death.Value = data["Deaths"].Value
		kill.Value = data["Kills"].Value
	else
		cash.Value = 0
		death.Value = 0
		kill.Value = 0
	end
end

local function createtable(player)
	local player_stats = {}
	for _,stats in pairs(player.leaderstats:GetChildren()) do
		player_stats[stats.Name] = stats.Value
	end
	return player_stats
end



local function onPlayerExit(player)  --Runs when players exit
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player.leaderstats.Cash.Value)
		playerData:SetAsync(playerUserId, player.leaderstats.Deaths.Value)
		playerData:SetAsync(playerUserId, player.leaderstats.Kills.Value)--Saves player data
	end)
	if not success then
		warn('Could not save data!')
	end
end


game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

This is a normal script inside serverscriptservice also im new to tables.

Can you expand on what ‘it doesn’t work’ means? Like, is it not saving data, loading data, getting the actual store etc

1 Like

it doesnt save/load but it does make the leaderstats itself

ServerScriptService.Leaderstats:49: attempt to index number with ‘Cash’ this error happens

This Is For The Leaderstats

local DSS = game:GetService("DataStoreService")
local myDataStore = DSS:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(plr)
local lead = Instance.new("Folder")
lead.Name = "leaderstats"
lead.Parent = plr

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = lead

local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = lead

local Deaths= Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = lead



local data
local succ, err = pcall(function()
	data = myDataStore:GetAsync(plr.UserId.."_cash", plr.leaderstats.Cash.Value)
            data = myDataStore:GetAsync(plr.UserId.."_Kills", plr.leaderstats.Kills.Value)
            data = myDataStore:GetAsync(plr.UserId.."_Deaths", plr.leaderstats.Deaths.Value)
end)	

if succ then
	Cash.Value = data	
            Kills.Value = data
            Deaths.Value = data
	print("Succesfully got the data!")
end
end)


game.Players.PlayerRemoving:Connect(function(plr)

local data = plr.leaderstats.Cash.Value

local succ, err = pcall(function()
		data = myDataStore:GetAsync(plr.UserId.."_cash", plr.leaderstats.Cash.Value)
            data = myDataStore:GetAsync(plr.UserId.."_Kills", plr.leaderstats.Kills.Value)
            data = myDataStore:GetAsync(plr.UserId.."_Deaths", plr.leaderstats.Deaths.Value)
end)

if succ then
	print("Succesfully saved the data!")
else
	print("There was an error while saving data")
	warn(err)
end
end)

Put This Is Another Script, It Will Auto Save

local player = game:GetService(“Players”)

player.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
local tag = character.Humanoid:FindFirstChild(“creator”)
if tag ~= nil then
local player = tag.Value
local bounty = 1
local leaderstats = player:WaitForChild(“leaderstats”)
leaderstats.Kills.Value += bounty
leaderstats.Cash.Value += 100
end
end)
player.CharacterAdded:Connect(function(char)

	local humanoid = char:FindFirstChild("Humanoid")

	humanoid.Died:Connect(function()
		local leaderstats = player:WaitForChild("leaderstats")
		leaderstats.Deaths.Value += 1
	end)
end)

end)

How does it save to the table?

I believe this code may help:

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("TableSavingDataStore")

local function onPlayerJoin(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Parent = leaderstats
	
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	local plrUserId = "Player_"..player.UserId
	local Data
	
	pcall(function()
		Data = DataStore:GetAsync(plrUserId)
	end)
	
	if Data ~= nil then
		Cash.Value = Data["Money"] -- Make sure these are in order of creation ie: cash, kills, deaths
		Kills.Value = Data["XP"]
		Deaths.Value = Data["level"]
		print("Loaded: "..player.Name.." Data")
	else
		Cash.Value = 50
		Kills.Value = 0
		Deaths.Value = o
		print(player.Name.." Is A New User")
	end
	
end

local function createTable(player)
	local playerStats = {}
	for i, stat in pairs(player.leaderstats:GetChildren()) do
		playerStats[stat.Name] = stat.Value
	end
	return playerStats
end

local function onPlayerExit(player)
	local playerStats = createTable(player)
	local success, err = pcall(function()
		local plrUserId = "Player_"..player.UserId
		DataStore:SetAsync(plrUserId, playerStats)
	end) 
	
	if not success then
		print("Could not save data: "..tostring(err))
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

nevermind i fixed it myself thanks for help