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 make this script autosave when the player leaves?

local chars = game.ReplicatedStorage:WaitForChild("Characters")

local classNames = {"Accessory", "Shirt", "Pants", "ShirtGraphic", "BodyColors"}


game.Players.PlayerAdded:Connect(function(plr)
	
	local ls = Instance.new("Folder", plr)
	ls.Name = "leaderstats"
	
	local cash = Instance.new("IntValue", ls)
	cash.Name = "Bolts"
	cash.Value = 0
	
	local chars = Instance.new("Folder", plr)
	chars.Name = "OwnedCharacters"
end)


game.ReplicatedStorage.CharacterRE.OnServerEvent:Connect(function(plr, isBuying, character)
	
	if not chars:FindFirstChild(character) then return end
	
	if isBuying and not plr.OwnedCharacters:FindFirstChild(character) then
		
		local price = chars:FindFirstChild(character).Price.Value
		local plrCash = plr.leaderstats.Cash
		
		if price <= plrCash.Value then
			
			plrCash.Value -= price
			
			chars[character]:Clone().Parent = plr.OwnedCharacters
		end
		
		
	elseif not isBuying and plr.OwnedCharacters:FindFirstChild(character) and plr.Character and plr.Character:FindFirstChild("Humanoid") then
		
		for i, descendant in pairs(plr.Character:GetDescendants()) do
			
			if table.find(classNames, descendant.ClassName) or descendant:IsA("Decal") and descendant.Parent.Name == "Head" then

				descendant:Destroy()
			end
		end
		
		
		for i, descendant in pairs(plr.OwnedCharacters[character]:GetDescendants()) do
			
			if table.find(classNames, descendant.ClassName) then
				
				descendant:Clone().Parent = plr.Character
				
			elseif descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
				descendant:Clone().Parent = plr.Character.Head
			end
		end
	end
end)

I also use a kit from a youtuber named (HowToRoblox) A Character Shop Gui.. And i want it to autosave when the player leaves.. I just dont know how im going to make the script save the stats..

1 comment

2 answers

You can use datastoreservice to save your players data. For the shop, save the items you have bought inside a table and change the shop script to check if the item is inside that table.

You can see when the player leaves with playerremoving

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