local key = 1
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("toolSaveStore"..key)
local toolStorage = game:GetService("ServerStorage"):WaitForChild("ShopItems") --The folder might not be loaded it so we use WaitForChild just in case

local function onPlayerAdded(Player)
	local Data
	local suc,err = pcall(function() --Make a pcall function
		Data = DataStore:GetAsync(Player.UserId)
	end)
	if suc then
		print("Success! -- toolsave script") --If the pcall was a success we let the server know
	else
		print("Error! -- toolsave script") --If the pcall was erroring we let the server know
	end
	if Data then --If the player has tools saved then
		for _,v in pairs(Data) do
			for _,t in pairs(toolStorage:GetChildren()) do --Iterate through the folder
				if v == t.Name then --If the tool name and the player data tools match then
					t:Clone().Parent = Player.StarterGear -- loads tools in starter gear
					if not Player.Backpack:FindFirstChild(t.Name) then
						t:Clone().Parent = Player.Backpack -- loads the tools into backpack
					end
				end
			end
		end
	else
		if not Player.Backpack:FindFirstChild("NormalWeight") then --Replace TheTool with your tool name
			print("Error with saving tools last time!")
			toolStorage.NormalWeight:Clone().Parent = Player.Backpack
			toolStorage.NormalWeight:Clone().Parent = Player.StarterGear
			game.ReplicatedStorage.Remotes.ErrorRemote:FireServer("There was an Error: saving tools last time you left.")
		end
	end
end

local function onPlayerRemoved(Player)
	local DataToSave = {}
	for _,v in pairs(Player.StarterGear:GetChildren()) do
		table.insert(DataToSave, v.Name)
	end
	local suc,err = pcall(function()
		DataStore:SetAsync(Player.UserId, DataToSave)
	end)
end

game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)
game:GetService("Players").PlayerRemoving:Connect(onPlayerRemoved)

game:BindToClose(function()
	for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
		onPlayerRemoved(Player)
	end
end)

i dont get any issues when playing api service is on

4 comments

0

is api service on?

0

the code seems fine, this is weird

0

did you test it on roblox studio? because roblox studio don't save data stores, you must test it on real roblox server.

0

yes it is jesse