Datastore Not working

My datastore for my game has not been working, and im not sure why, it doesn’t seem to output any errors either.

Here is my code.

local DS = game:GetService("DataStoreService")
local PS = game:GetService("Players")
local RS = game:GetService("RunService")
local Moods = DS:GetDataStore("SCP_INC_PLR_MOODS")

local WebHooks = require(game.ReplicatedStorage.Modules.Webhooks)

PS.PlayerAdded:Connect(function(pl)
	local MOODfolder = Instance.new("Folder")
	MOODfolder.Name = "MoodData"
	MOODfolder.Parent = pl
	
	local godVal = Instance.new("BoolValue")
	godVal.Name = "God"
	godVal.Parent = MOODfolder
	
	local thirstVal = Instance.new("NumberValue")
	thirstVal.Name = "Thirst"
	thirstVal.Parent = MOODfolder
	
	local hungerVal = Instance.new("NumberValue")
	hungerVal.Name = "Hunger"
	hungerVal.Parent = MOODfolder
	
	local dirtinessVal = Instance.new("NumberValue")
	dirtinessVal.Name = "Dirtiness"
	dirtinessVal.Parent = MOODfolder
	
	local fatigueVal = Instance.new("NumberValue")
	fatigueVal.Name = "Fatigue"
	fatigueVal.Parent = MOODfolder
	
	local success_MOOD_Thirst, Thirst_Data = pcall(function()
		Moods:GetAsync(pl.UserId.."-SCP_INC_MOOD_THIRST")
	end)
	local success_MOOD_Hunger, Hunger_Data = pcall(function()
		Moods:GetAsync(pl.UserId.."-SCP_INC_MOOD_HUNGER")
	end)
	local success_MOOD_Dirtiness, Dirtiness_Data = pcall(function()
		Moods:GetAsync(pl.UserId.."-SCP_INC_MOOD_DIRTINESS")
	end)
	local Success_MOOD_Fatigue, FatigueData = pcall(function()
		Moods:GetAsync(pl.UserId.."SCP_INC_MOOD_FATIGUE")
	end)
	
	if success_MOOD_Thirst then
		thirstVal.Value = Thirst_Data
	else
		thirstVal.Value = 0
	end
	if success_MOOD_Hunger then
		hungerVal.Value = Hunger_Data
	else
		hungerVal.Value = 0
	end
	if success_MOOD_Dirtiness then
		dirtinessVal.Value = Dirtiness_Data
	else
		dirtinessVal = 0
	end
	if Success_MOOD_Fatigue then
		fatigueVal.Value = FatigueData
	else
		fatigueVal.Value = 0
	end
end)

PS.PlayerRemoving:Connect(function(pl)
	local MOODfolder = pl:WaitForChild("MoodData")
	local ThirstVal = MOODfolder:WaitForChild("Thirst")
	local HungerVal = MOODfolder:WaitForChild("Hunger")
	local DirtinessVal = MOODfolder:WaitForChild("Dirtiness")
	local FatigueVal = MOODfolder:WaitForChild("Fatigue")
	
	-- // Setting in Data
	local ThirstSuccess, ErrorThirst = pcall(function()
		Moods:SetAsync(pl.UserId.."-SCP_INC_MOOD_THIRST",ThirstVal.Value)
	end)
	local HungerSuccess, ErrorHunger = pcall(function()
		Moods:SetAsync(pl.UserId.."-SCP_INC_MOOD_HUNGER",HungerVal.Value)
	end)
	local DirtinessSuccess, ErrorDirtiness = pcall(function()
		Moods:SetAsync(pl.UserId.."-SCP_INC_MOOD_DIRTINESS")
	end)
	local FatigueSuccess, ErrorFatigue = pcall(function()
		Moods:SetAsync(pl.UserId.."SCP_INC_MOOD_FATIGUE",FatigueVal.Value)
	end)
	
	-- // Communicating with the console for any possible save errors, then sending them over to the discord
	if not ThirstSuccess and not RS:IsStudio() then
		WebHooks.SendEmbedFeild2(WebHooks.Hooks.SaveFailLogs.Token,"**Save Error**","Possible fail in saving a user's data.",tonumber(0xf30000),"Username","Userid",pl.Name,pl.UserId)
	end
	if not HungerSuccess and not RS:IsStudio() then
		WebHooks.SendEmbedFeild2(WebHooks.Hooks.SaveFailLogs.Token,"**Save Error**","Possible fail in saving a user's data.",tonumber(0xf30000),"Username","Userid",pl.Name,pl.UserId)
	end
	if not DirtinessSuccess and not RS:IsStudio() then
		WebHooks.SendEmbedFeild2(WebHooks.Hooks.SaveFailLogs.Token,"**Save Error**","Possible fail in saving a user's data.",tonumber(0xf30000),"Username","Userid",pl.Name,pl.UserId)
	end
	if not FatigueSuccess and not RS:IsStudio() then
		WebHooks.SendEmbedFeild2(WebHooks.Hooks.SaveFailLogs.Token,"**Save Error**","Possible fail in saving a user's data.",tonumber(0xf30000),"Username","Userid",pl.Name,pl.UserId)
	end
end)

The Values are not Specified, you will need to specify what they are, as the variables are not within the same scope.

Also, You shouldn’t be creating that many keys to save one piece of Data, you should Instead be creating one Key, with a table storing all that Data.

Indeed, it will help if you optimize your script to begin with. Something I want tk make clear though is ‘’ S.PlayerRemoving:Connect(function(pl) local MOODfolder = pl:WaitForChild ‘’ might cause some issues, if the player is leaving all assests pertaining to said player will be removed, so when you wait for it there is a chance to end up with a infinite yield resulting in data loss there. It is better to retrieve the objects before removing so you can instantly pull all the values before they are removed.

You also arent using pcalls to their potential here. Pcalls are a good way of handling errors, but you need to properly handle the errors. So at current it will report the error to you but wont try again in case of failure, make it repeat 3 times before calling it off as a error, datastores can fail sometimes due to reasons outside of devs hands. Aside from that you are more or less on the right track

For optimising this I would create either a index or a table for the values so they can easily be accessed from a single variable when needed. Then for the data saving just take the values and stick em together, the good thing being is that it will come out in the same order you out them in, easy stuff there. And for the removing, this is very important, only pull the required data when removing, then save outside the removing function to reduce the chance of not retrieving all the data you want to be saved.

You need to do return statements for the GetAsync to get the value of the results..

Not return statements but variables. But yes.

1 Like

I found out my issue, I forgot to use return statements like @hasoco mentioned, and I was changing the mood data via the client which didn’t send information to the server when the stats were saved resulting with them being turned into a default value of 0. I also utilized what @minimic2002 stated about using tables to orginize my datastore.

Here is the code for anybody interested.

local DS = game:GetService("DataStoreService")
local PS = game:GetService("Players")
local RS = game:GetService("RunService")
local Moods = DS:GetDataStore("REDACTED")
local Bindables = game.ReplicatedStorage.Bindables
local Util = require(game.ReplicatedStorage.Modules.Util)

PS.PlayerAdded:Connect(function(pl)
	
	-- // Setting up mood instances
	local MOODfolder = Instance.new("Folder")
	MOODfolder.Name = "MoodData"
	MOODfolder.Parent = pl
	local godVal = Instance.new("BoolValue")
	godVal.Name = "God"
	godVal.Parent = MOODfolder
	local thirstVal = Instance.new("NumberValue")
	thirstVal.Name = "Thirst"
	thirstVal.Parent = MOODfolder
	local hungerVal = Instance.new("NumberValue")
	hungerVal.Name = "Hunger"
	hungerVal.Parent = MOODfolder
	local dirtinessVal = Instance.new("NumberValue")
	dirtinessVal.Name = "Dirtiness"
	dirtinessVal.Parent = MOODfolder
	local fatigueVal = Instance.new("NumberValue")
	fatigueVal.Name = "Fatigue"
	fatigueVal.Parent = MOODfolder
	
	--[[
	// Mood Store Setup
		[1] - Thirst
		[2] - Hunger
		[3] - Dirtiness
		[4] - Fatigue
	--]]
	
	local SuccessMoods, MoodData = pcall(function()
		return Moods:GetAsync(pl.UserId.."-SCP_MOOD_DATA")
	end)
	
	if SuccessMoods then
		if MoodData ~= nil then
			thirstVal.Value = MoodData[1]
			hungerVal.Value = MoodData[2]
			dirtinessVal.Value = MoodData[3]
			fatigueVal.Value = MoodData[4]
		else
			thirstVal.Value = 0
			hungerVal.Value = 0
			dirtinessVal.Value = 0
			fatigueVal.Value = 0
		end
	else
		pl:Kick("Error grabbing data. Please rejoin. If this issue occurs report it to a developer in the communications server or on other non personal socials.")
	end
	
end)

PS.PlayerRemoving:Connect(function(pl)
	
	local MOODfolder = pl:WaitForChild("MoodData")
	local ThirstVal = MOODfolder:WaitForChild("Thirst")
	local HungerVal = MOODfolder:WaitForChild("Hunger")
	local DirtinessVal = MOODfolder:WaitForChild("Dirtiness")
	local FatigueVal = MOODfolder:WaitForChild("Fatigue")
	
	local MoodTableCollection = {
		[1] = ThirstVal.Value;
		[2] = HungerVal.Value;
		[3] = DirtinessVal.Value;
		[4] = FatigueVal.Value
	}
	
	local SuccessMoods, ErrorMoods = pcall(function()
		Moods:SetAsync(pl.UserId.."-SCP_MOOD_DATA",MoodTableCollection)
	end)
	
	if not SuccessMoods and not RS:IsStudio() then
		Bindables:WaitForChild("SendEmbedFeild2"):Fire("SaveFailLogs","**Possible DataSave Error**","A possible DataSave error has occured at "..Util.GetTimeInEST().." Eastern standard time.",tonumber(0xcb1d1d),"Username","UserID",pl.Name,pl.UserId)
	end
	
end)

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