Datastore troubles

Hiya developers,

Im not great with datastores. its not something i really meddle with, but its come to that point.

My issue is the datastores wont save, if i go to server and change the values. it does not save.

“statdata”, or well the database was never created so i have no idea if thats why.


local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local database = DatastoreService:GetDataStore("StatData")
local sessionData = {}


--Functions
--playeradded
function PlayerIsAdded(player)
	
	
	
	print(player.Name,"has joined the server.")

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	

	
	

	local Strength = Instance.new("IntValue")
	Strength.Name = "Strength" --power
	Strength.Parent = leaderstats
	
	local Agility = Instance.new("IntValue")
	Agility.Name = "Agility" --Tier
	Agility.Parent = leaderstats
	
	local Fortitude = Instance.new("IntValue")
	Fortitude.Name = "Fortitude" --rebirth
	Fortitude.Parent = leaderstats
	
	local success = nil
	local PlayerData = nil
	local attempt = 1
	
	repeat
		success, PlayerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)
		attempt +=1
		if not success then
			warn(PlayerData)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print("Connected to database")
		if not PlayerData then
			print("Assigning default data")
			PlayerData = {
				["Strength"] = 0,
				["Agility"] = 0,
				["Fortitude"] = 0,
		


			}
		end
		sessionData[player.UserId] = PlayerData
	else
		warn("Unable to get data for",player.UserId)
		player:Kick("Unable to load data. Try again later.")
	end
	
	
	
	Strength.Value = sessionData[player.UserId].Strength
	Strength.Changed:Connect(function()
		sessionData[player.UserId].Strength = Strength.Value
	end)
	
	Agility.Value = sessionData[player.UserId].Agility
	Agility.Changed:Connect(function()
		sessionData[player.UserId].Agility = Agility.Value
	end)
	
	Fortitude.Value = sessionData[player.UserId].Fortitude
	Fortitude.Changed:Connect(function()
		sessionData[player.UserId].Fortitude = Fortitude.Value
	end)
	leaderstats.Parent = player


end
--player leaving
function PlayerLeaving(player)
	print(player.Name,"has left the server.")
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1
		
		
		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			attempt +=1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5
		
		if success then
			print("Data saved for", player.Name)
		else
			warn("Unable to save data for",player.Name)
		end
		
	end
end


Players.PlayerAdded:Connect(PlayerIsAdded)
Players.PlayerRemoving:Connect(PlayerLeaving)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Add this to the end of the script to give the game time to save:

game:BindToClose(function()
    task.wait(3)
end)

If that doesn’t work, test your game outside of Studio.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.