DataStore2 Help

So, I am making a “Eating Simulator” because I thought it would be fun and I’m trying to use DataStore2 but it is telling me:
Data store food was not saved as it was not updated.
I am new to data store so I don’t know what is wrong… (Yes I do know I am using a remote event to increment the players data and this is not safe I will change that later.)

local plrStats = game:GetService("ServerStorage"):WaitForChild("PlrStats")
local DSS = game:GetService("DataStoreService")
local ds1 = DSS:GetDataStore("FoodSave")
local debris = game:GetService("Debris")

local DataStore2 = require(game:GetService("ServerStorage"):WaitForChild("MainModule"))

local defaultFood = 0


local tipTemp = script:WaitForChild("Tip")

function createTip(plr, text)
	if plr:IsA("Player") and plr:FindFirstChild("PlayerGui") then
		pcall(function()
			local new = tipTemp:Clone()
			new.Parent = plr:FindFirstChild("PlayerGui")
			new:FindFirstChild("TextLabel").Text = "Congrats, " .. plr.Name .. " on reaching " .. tostring(text) .. " food!!!"
			
			debris:AddItem(new, 5)
			
		end)
		
	end
end


game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plrStats:FindFirstChild(plr.Name) then plrStats:FindFirstChild(plr.Name):Destroy() end
	
	local foodDataStore = DataStore2("food", plr)
		
	local stats = Instance.new("Folder")
	stats.Name = plr.Name
	stats.Parent = plrStats
	
	wait()
	
	local rFood = Instance.new("IntValue")
	rFood.Name = "rFood"
	rFood.Parent = stats
	
	wait()
	
	local ID = Instance.new("IntValue")
	ID.Name = "UserId"
	ID.Value = plr.UserId
	ID.Parent = stats
	
	wait()
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	wait()

	local food = Instance.new("IntValue")
	food.Name = "Food"
	food.Parent = leaderstats
	
	
	food.Changed:Connect(function()
		if food.Value == 100 then
			createTip(plr, 100)
		end
	end)
	
	
	local function foodUpdate(updatedValue)
	
		food.Value = foodDataStore:Get(updatedValue)
		
	end
	
	foodUpdate(defaultFood)
	
	foodDataStore:OnUpdate(foodUpdate)
	
	spawn(function()
		while wait() do
			food.Value = (rFood.Value)
		end
	end)
end)


local event = game:GetService("ServerStorage"):WaitForChild("AwardStats")
--local cEvent = game:GetService("ReplicatedStorage"):WaitForChild("cAwardStats")

event.OnServerEvent:Connect(function(plr, stat, value)
	pcall(function()
		if plr:IsA("Player") then
			local plrStat = game:GetService("ServerStorage"):FindFirstChild("PlrStats"):FindFirstChild(plr.Name):FindFirstChild(tostring(stat))
			local foodDataStore = DataStore2("food", plr)
			
			foodDataStore:Increment(value * 10, defaultFood)
			
		end
	end)
end)

So I probably am being dumb but I’m tired so oh well… XD

EDIT: Yes I do have a value in studio set to true called “SaveInStudio”

2 Likes

It should be displaying a message that it saved in the end.

Did you forget about using DataStore2:Save()?

I can also notice performance impacts on the scripts and security issues.

Data store food was not saved as it was not updated. Means that You’ve haven’t changed the data yet.

When changing stats and other data, its recommended that you should do it on the DataStore itself. Essentialy handle all data on the script that its contained on.

Adding a bunch of those wait()'s are also very unnecessary.

You should also put the datastore module on the serverscriptservice (Just an opinion though).

The incrementing data should work as long as you’ve called the Remote Event correctly. (Correct me if i’m wrong)

Ok, so I looked at the tool and it turns out that I am not actually using the Remote Event at all. I must have forgotten to remove it from the server script… :see_no_evil:

Weird… I have this script as my tool:

--Written by: GamingBroCoolkid328

script.Parent:WaitForChild("PassPlayer").OnServerEvent:Connect(function(player)	plr = player end)
repeat wait() until plr and plr:IsA("Player")
local tool = script.Parent
local cooldown = 0.5 -- The number of seconds between clicks
local db = false
local food = game:GetService("ServerStorage"):WaitForChild("PlrStats"):WaitForChild(plr.Name):WaitForChild("rFood")
local sEvent = script.Parent:WaitForChild("StartStopSound")
local eatCooldown = 0.7
local eDb = false

local DataStore2 = require(1936396537)

local defaultFood = 0

local foodDataStore = DataStore2("food", plr)

function foodUpdate(updatedValue)
	
	food.Value = foodDataStore:Get(updatedValue)
		
end

foodUpdate(defaultFood)

foodDataStore:OnUpdate(foodUpdate)

tool.Activated:Connect(function()
	if db == false then
		db = true
		
		foodDataStore:Increment(1, defaultFood)
		
		wait(cooldown)
		
		db = false
	end
end)

spawn(function()
	tool.Activated:Connect(function()
		if eDb == false then
			eDb = true
			sEvent:FireClient(plr, "play")
			wait(eatCooldown)
			sEvent:FireClient(plr, "stop")
			eDb = false
		end
	end)
end)

And I got some error about the script which has created this callback error has been destroyed or something, then, studio wouldn’t stop the test so… Thanks for helping me I will try to figure this out some more. Mabey I will only use Data Store 2 when I know a little bit more about it. :smiley:

Do you mean :Set()? :Get() gets the data, :Set() sets it.

No, I was setting the food value to the data from the data store.