CropSaving script not working

So i did a post before that talked about an error in this script and it is fixed but now i have another problem now it doesn’t save and i was wondering if anyone could help me

local DataStore = game:GetService("DataStoreService")
local Data = DataStore:GetDataStore("Cropv10")

local RS = game:GetService("ReplicatedStorage")
local Crops = RS:WaitForChild("Crops")
local function GetCrop(player)
	coroutine.resume(coroutine.create(function()
		
	for i,v in pairs(player.Crops:GetChildren()) do
			local Items = Data:GetAsync(player.UserId)
			print("Starting")
			if Items then
				for i,item in pairs(Items) do
					local ItemName = item[1]
					print(ItemName)
					local CloneItem = Crops:FindFirstChild(ItemName):Clone()
					CloneItem.Parent = player:WaitForChild("Crops")
					

				end
			
			return
	end
	end
	end))
end

local function GrabCrop(player)
local Items = {}
for i,v in pairs(player.Crops:GetChildren()) do
			
	for i,item in pairs(player.Crops:GetChildren()) do
			
		if item:IsA("Part") or item:IsA("MeshPart") then	
			print("Saving")		
			table.insert(Items, item.Name)		
        end
	end
	wait()
end
Data:SetAsync(player.UserId,Items)
end

game.Players.PlayerAdded:Connect(function(Player)
	wait(3)
	GetCrop(Player)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	GrabCrop(Player)
end)

Does it print anything when you leave?

Yes it does and i tried making them print the items name when leaving it said the item’s name correctly but when u enter it prints nil

and then i added the if statement which is this

if Items then
				for i,item in pairs(Items) do
					local ItemName = item[1]
					print(ItemName)
					local CloneItem = Crops:FindFirstChild(ItemName):Clone()
					CloneItem.Parent = player:WaitForChild("Crops")
					

				end 

and it didn’t detect any data

Try making it save every dozen seconds and see what happens because studio terminates the server at the same time it terminates the client, so it doesn’t get to save data

Screenshot_85

Now it detected data but it didn’t get the item’s name

why do you have

local ItemName = item[1]

shouldn’t it be

local ItemName = item

because you saved the item name, not a table

but when the player leaves it saves it to a table

local Items = {}
for i,v in pairs(player.Crops:GetChildren()) do
			
	for i,item in pairs(player.Crops:GetChildren()) do
			
		if item:IsA("Part") or item:IsA("MeshPart") then	
			print("Saving")		
			table.insert(Items, item.Name)		
        end
	end
	wait()
end

or is it something different

and it saves the table itself

Data:SetAsync(player.UserId,Items)

May I ask why you are getting data in every loop interval?

	for i,v in pairs(player.Crops:GetChildren()) do
			local Items = Data:GetAsync(player.UserId)

Also can you please print the contents of the Items, as it is obvious that something isn’t being indexed correctly.

EDIT

I see this

for i,item in pairs(player.Crops:GetChildren()) do
			
		if item:IsA("Part") or item:IsA("MeshPart") then	
			print("Saving")		
			table.insert(Items, item.Name)		
        end
	end

And I am unsure as to whether table.insert(x,y) will make a new table within the table?

Referencing table | Documentation - Roblox Creator Hub I am sure it just inserts the value.

Screenshot_87
This is what i get when i go ingame

Screenshot_88
and this is the place where it checks if player has the same stuff inside of it

I had it as

table.insert(Items,{item.Name})

in the beginning

Please can you print the actual values of the table.

for i,item in pairs(Items) do		
print(item)
end

i made it

table.insert(Items,{item.Name})

and now it works thank you :smiley: :+1:

That’s fine but if you have no reason to do that it may be simpler to just insert the value?

I don’t know if there is any performance drop nonetheless.

So what u are saying is it would be more efficient if i made it insert values?

Also please don’t get the same data multiple times in a for loop, if you haven’t fixed that already.

CONT

I do not know, you normally use tables if you are saving multiple pieces of data. It may take more bytes for the jsonencoder to save, but this is minute in scale. If you are just saving names then a list of names, not a list of lists of names, would probably be less confusing and redundant.

like that

for i,item in pairs(player.Crops:GetChildren()) do
			
		if item:IsA("Part") or item:IsA("MeshPart") then	
			print("Saving")		
			table.insert(Items,{item.Name})
	
			print(item.Name)	
        end
	end
	wait()
end
Data:SetAsync(player.UserId,Items)
end

It may be easier if I represent it -

You are currently saving your data as -

{
{name},
{name}
}

This is unneeded, you want it more like

{
name,
name
}

Also yes, just get the data you want once!

1 Like

okay i get it now this is way better and simpler thank you

1 Like