Problem with Data Storing

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? The Value of Fruits is not saving, however The Value of Cash is saving as it should.

  3. What solutions have you tried so far? Looked at devforum, couldn’t find any solution, editing the script.

Leaderstats Script

local ServerStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("NumberValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local fruits = Instance.new ("IntValue")
	fruits.Name = "Fruits"
	fruits.Parent = leaderstats
	
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = player.Name
	dataFolder.Parent = ServerStorage.RemoteData
	
	local debounce = Instance.new("BoolValue")
	debounce.Name = "Debounce"
	debounce.Parent = dataFolder
	
	local cashData, fruitsData
	
	local success,errormessage = pcall(function()
		cashData = DataStore:GetAsync("Cash-"..player.UserId)
		fruitsData = DataStore:GetAsync ("Fruits -"..player.UserId)
		
		
	end)
		if success then
		if cashData then
		cash.Value = cashData
			fruits.Value = fruitsData
			print("Game Data Loaded!")
		else
			print("There was an error getting your data")
			warn(errormessage)
		end
	end
end)

		
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall (function()
		DataStore:SetAsync("Cash-"..player.UserId, player.leaderstats.Cash.Value)
		DataStore:SetAsync ("Fruits-"..player.UserId, player.leaderstats.Fruits.Value)
	end)
	if success then
		print("Player data saved!")
	else
		print("There was an error saving your data")
		warn(errormessage)
	end
	
end)

and the script that adds Fruits.

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local showwarning = game.ReplicatedStorage.Remotes.ShowWarning

local cooldown = 1


replicatedStorage.Remotes.CollectFruit.onServerEvent:Connect(function(player)
	

	
	if not remoteData:FindFirstChild(player.name) then return "NoFolder" end
	
	local debounce = remoteData[player.name].Debounce
	
	if not debounce.Value then
		debounce.Value = true
		
		player.leaderstats.Fruits.Value = player.leaderstats.Fruits.Value + 1
		if player.leaderstats.Fruits.Value > 10 then
			showwarning:FireClient(player) -- this just shows gui if you want to sell fruits
			player.leaderstats.Fruits.Value = 10
		end
		
		wait (cooldown)
		
		debounce.Value = false
		
	end
	
end)

Your fruits datastore names are different. When you’re saving data, you save to the Fruits- datastore, but when you retrieve that data when a player enters, you’re retrieving from the Fruits - datastore (notice the space between “Fruits” and the dash).

Additionally, when you’re retrieving data from your fruits datastore, you have a space between DataStore:GetAsync and your query. I don’t recall if this causes an error, but is just something I noticed.

1 Like