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

How to save Models parent with data store service?

so im making an upgraded version of a regular tycoon and im trying to add saving items. So as i know you cant save model directly through data store service so i tried to save its parent (which should work right) but for some reason it isnt.

I tried to do it with

for i,v in pairs() do

but it didnt work either. So heres the script, also i would REALLY appreciate if someone knows a shorter way to do it, instead of adding several new lines for each new item.

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("DS2")
local basicdropper = game.ServerStorage.PURCHASABLES.BasicDropper
game.Players.PlayerAdded:Connect(function(plr)
	local success,err = pcall(function()
		basicdropper.Parent = ds2:GetAsync(plr.UserId)
	end)
	if success then
		print("Your Items Have Been Loaded.")
	else
		print("Your Items Loading Has Failed.")
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		ds2:SetAsync(plr.UserId, basicdropper.Parent)
	end)
	if success then
		print("Your Items Have Been Saved.")
	else
		print("We Got An Error While Saving Your Items.")
		warn(err)
	end
end)

1 answer

Save the model's name as a string with tostring() then load the string into a variable (In this case named PurchasedItem) and finally find the model with

game.ServerStorage.PURCHASABLES[PurchasedItem]

Ex.

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("DS2")
local ItemBin = -- wherever you want to store items

game.Players.PlayerAdded:Connect(function(plr)
    local success,err = pcall(function()
         local Item = game.ServerStorage.PURCHASABLES[ds2:GetAsync(plr.UserId)]:Clone()
	 Item.parent = ItemBin 
    end)
    if success then
        print("Your Items Have Been Loaded.")
    else
        print("Your Items Loading Has Failed.")
        warn(err)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success,err = pcall(function()
        ds2:SetAsync(plr.UserId, ItemBin:FindFirstChild().Name)
    end)
    if success then
        print("Your Items Have Been Saved.")
    else
        print("We Got An Error While Saving Your Items.")
        warn(err)
    end
end)

16 comments

0

do you think it is possible to make it shorter?

0

i mean is it possible to make it so i dont have to add multiple lines for each new item?

0

@TFlanigan you wont need to add a line for each item

0

huh? what do you mean?

0

how

0

@TFlanigan how would you use this in a script, cause you shouldent need to add a line for every item

0

?

0

Let me add an example to the answer

0

?

0

oh ok

0

wait what the hell i thought u cant save models

0

Wait wait wait, So what is itembin? Should it be purchased items or purchasables? or both at the same time?

0

idk if im doing something wrong but i get this: http://prntscr.com/qwl36j

0

what im doing is saving the name of the model and using it to find the model in the next session

0

ItemBin would be like your inventory

0

ooh

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