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

How can I save a player's gear that they received? (Read Desc)

I have an in-game shop where players can spend points on fun gears. I tested this by purchasing a gear for fifty points. I received the gear, but when I left the game and returned.. the gear had gone, along with the points to buy it.

Help?

1 comment

2

Read the Data Persistence/Data Store article in Wikipedia.

1 answer

This will save the gear to data store as a encoded table. This script can go anywhere in the game as long as it is not disabled. MAKE SURE: The gear is in the player`s startergear.

local ds=game:GetService("DataStoreService"):GetDataStore("Gear")  --Get`s the DataSoreService
function savegear(player)
	local gear={ } --Sets up a table for use later
	for i,v in pairs(player.StarterGear:GetChildren())do --Gets the gear from the players starter gear
		table.insert(gear,#gear+1,v) --Inserts gear into table
	end
	return gear --Returns the table filled with gear
end

game.Players.PlayerRemoving:connect(function(plr)
	savedgear=savegear(plr) --Saves gear
	ds:SetAsync(plr.userId.."_Gear",game.HttpService:JSONEncode(savedgear)) --Sets the table as a string
end)

game.Players.PlayerAdded:connect(function(plr)
	loadedgear=game.HttpService:JSONDecode(ds:GetAsync(plr.userId.."_Gear")) --Decodes the table
	if loadedgear~=nil then --Makes sure there is a value
		for i,v in pairs(loadedgear)do
			v.Parent=plr.StarterGear --Loads gear in player`s startergear
		end
	end
end)

Observe this code, edit it to save points instead of gear. CAUTION: This might work no guarantees, feel free to edit the code if needed.

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