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

how to make a table that will have names of all things player has bought?

so im making an upgraded version of regular tycoon and i have to make a script that saves things player has bought. i was trying to do it multiplie ways like saving their parents as a string, but none worked. if the game wont have a saving script its gonna die so im kinda worried. heres my current script that tries to save their parent but it doesnt work:

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("DS2")
local purchasables = game.ServerStorage.PURCHASABLES:GetChildren()

game.Players.PlayerAdded:Connect(function(plr)
    local success,err = pcall(function()
         for i,v in pairs(purchasables) do
       	 	v.Parent = ds2:GetAsync(plr.UserId)
		end
    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()
		for i,v in pairs(purchasables) do
            ds2:SetAsync(plr.UserId, v.Parent)
        end
    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

What you can do is, have a table that will contain a bunch of keys (I recommend you check them out. And each time the player buys something, that something would be added to the table. And once the player joins, that table will be retrieved, you would loop through it, and each time you find the name of an item that was bought, automatically unlock it.

local bought = {}

button.Touched:Connect(function() --just assuming this is how something is bought, and "button" is the bought object
	bought[button.Name] = true --this is how to create a key
	datastore:SetAsync(plr.UserId, bought) --save it
end)

players.PlayerAdded:Connect(function(plr) --when player enters
	bought = datastore:GetAsync(plr.UserId) --retrieve the table
end)

if bought[the name of whatever the object you want is] then
--unlock that object
end

2 comments

0

huh

0

what were you expecting?

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