My datastore script isnt working for some reason

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!
    My Datastore script isnt working, and everything seems to be alright (to me at least), So i was hoping any of you could help me with it
  2. What is the issue? Include screenshots / videos if possible!

This is my current script

local Datastore = game:GetService("DataStoreService"):GetDataStore("SaveData")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Swing = Instance.new("IntValue", leaderstats)
	Swing.Name = "Swing"
	
	local Potion = Instance.new("StringValue", leaderstats)
	Potion.Name = "Potion"
	
	local Rank = Instance.new("StringValue", leaderstats)
	Rank.Name = "Rank"
	
	local datas = Datastore:GetAsync(player.UserId)
	
	
	if datas.Swing ~= nil then
		Swing.Value = datas.Swing
		print("swing")
	else
		Swing.Value = 0
	end
	if datas.Potion ~= nil then
		Potion.Value = datas.Potion
		print("potion")
	else
		Potion.Value = "Normal"
	end
	if datas.Rank ~= nil then
		Rank.Value = datas.Rank
		print("rank")
	else
		Rank.Value = "Noob"
	end
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	local savedData = {}
	
	for _,Child in pairs(player.leaderstats:GetChildren())do
		table.insert(savedData,Child.Value)
		print("added")
	end
	Datastore:SetAsync(player.UserId, savedData)
	print("Data saved") -- this doesnt print
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have enabled API , Tried looping through each value, checked a lot of posts, deleted a lot of plugins, tested in game, changed values in the server instead of client, but still no luck :confused:

Thanks in advance! :slight_smile:

1 Like

Theres a couple problems with this script.
#1 please don’t skip assigning variables to their services because it is just easier to read.

local DatastoreService = game:GetService("DataStoreService")
local MyDataStore = DatastoreService:GetDataStore("MyDataStore")

Your instancing of the values are fine, except for when you actually get to load the data. Also what you don’t have is a pcall function which is VERY VERY NEEDED.

--This is inside of the playeradded also you need the local data below this line to not error
	local Data
	local Key = "Player_" .. player.UserId
	
	local complete, failed = pcall(function()
		Data = MyDataStore:GetAsync(Key)
	end)
	
	if complete then
		print("test")
		if Data ~= nil then
			Swing.Value = Data.Swing
			print("swing")
			Potion.Value = Data.Potion
			print("potion")
			Rank.Value = Data.Rank
			print("rank")
		end
	else
		Swing.Value = 0
		Potion.Value = "Normal"
		Rank.Value = "Noob"
	end

Next is saving the data. Same thing here it also needs a pcall when it saves data to the datastore

--this is inside of playerremoving
	local leaderstats = player.leaderstats
	
	local savedData = {
		Swing = leaderstats.Swing.Value,
		Potion = leaderstats.Potion.Value,
		Rank = leaderstats.Rank.Value
	}
	
	local complete, failed = pcall(function()
		MyDataStore:SetAsync(Key, savedData)
		print("Data saved")
	end)
	
	if complete then
		for _, Child in pairs(player.leaderstats:GetChildren())do
			table.insert(savedData,Child.Value)
			print("added")
		end
	end

Also one last thing you have to have for datastores to actually work is going to game settings and enabling the API if you haven’t already but it seems like all you forgot was the pcalls. Also studio testing doesn’t really use playerremoving event when you press the stop button when testing. Two ways to get around is to use players.(yournamehere):Kick() and it will fire the event or to start a server while testing which might take a bit longer.

1 Like