Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Saving table to Datastore not working?

So i'm trying to save my table of objects and their locations to a datastore, but when I go to read the table. The only item in there is the first X coord of an item. I'm quite sure it's only saving one number then stopping. I'm not sure what's wrong though.

-- Saving Part --

local ItemStore = game:GetService("DataStoreService"):GetDataStore("itemstore")
script.Parent.MouseButton1Click:connect(function()
	player = script.Parent.Parent.Parent.Parent
	findItems = game.Workspace.Tycoons.GoldMine1.Objects:GetChildren()
	for i = #findItems, 1, -1 do
	objects = {}
	table.insert(objects,i-3,findItems[i].Name)
	table.insert(objects,i-2,tostring(findItems[i].Hitbox.Position.X))
	table.insert(objects,i-1,tostring(findItems[i].Hitbox.Position.Y))
	table.insert(objects,i,tostring(findItems[i].Hitbox.Position.Z))
print(objects[i-3].." "..objects[i-2].." "..objects[i-1].." "..objects[i])	
	end
	ItemStore:SetAsync("Details" .. player.Name, objects)
end)

Saving seems to work as when I print out the table I get this

Dual Dropper -38.5 5 60
Dual Dropper -44.5 5 54
BB -62.5 -0.5 99

-- Loading Part --

local objects = ItemStore:GetAsync("Details" .. player.Name)
if objects ~= nil then
for i = #objects, 1, -1 do
	print(objects[1])	
	Item = game.ReplicatedStorage.Items[objects[i-3]]:Clone()
	Item.PrimaryPart = Item.Hitbox
	coord1 = objects[i-2]
	coord2 = objects[i-1]
	coord3 = objects[i]
	Item:SetPrimaryPartCFrame(CFrame.new(coord1,coord2,coord2))
	Item.Parent = game.Workspace
	end
end

I'm getting this error when loading

Workspace.onPlayerAdded:5: bad argument #2 to '?' (string expected, got nil)

4 comments

0

Its nice that you give us an error, but it doesnt link to the script that you put in as it isnt the full script.

0

There's no need to give the full script. The error is referring to line 5 on the loading part of the script above.

0

Please tell me if my answer fixed your problem or not. If not, is it still gives same error output? Or is your question closed?

0

I managed to fix it. The table was being wiped clean at the start of every loop. I didn't realise I left objects={} in there.

1 answer

For Loop starts to count from #objects and goes to 1, right? So when i = 1, the line that is giving error tries to get objects[i-3] which is objects[-2] which is not there. If you REALLY need it to call i-3th variant, then try this instead of that for loop:

for i = #objects, 4, -1 do

This PROBABLY will fix your problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.