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

Tool save / load script won't work, unsure as to what is wrong with it?

I have a tool load and save script inside ServerScriptService and as follows the script won't work.

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackpackSave")

game.Players.PlayerAdded:Connect(function(player)
	pcall(function()
		local tool = datastore:GetAsync("User_"..player.UserId)
		if tool then
			for i,v in pairs(tool) do
				local toolFound = game.ReplicatedStorage.Tools:FindFirstChild(v)
				if toolFound then
					toolFound:Clone().Parent = player.Backpack
					toolFound:Clone().Parent = player.StarterGear
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		local toolsave = {}
		for i, tool in pairs(player.Backpack:GetChildren()) do
			if tool then
				table.insert(toolsave,tool.Name)
			end
		end
		datastore:SetAsync("User_"..player.UserId,toolsave)
	end)
end)

What the script does is find tools inside a folder named Tools inside ReplicatedStorage and clone them and then save them inside their StarterGear, so the next time the player joins they will receive their tools. I, for some reason, can't find any errors here. API services are enabled.

Quick update: I have tried replacing a few things and it seems as if the search for the tool in replicatedstorage is thrown on an indefitnite yield.

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("BackpackSave")

game.Players.PlayerAdded:Connect(function(player)
    pcall(function()
		print("Save script functions")
        local tool = datastore:GetAsync("User_"..player.UserId)
        if tool then
            for i,v in pairs(tool) do
                local toolFound = game.ReplicatedStorage.Tools:WaitForChild(v)
				print(v)
                if toolFound then
                    toolFound:Clone().Parent = player.Backpack
                    toolFound:Clone().Parent = player.StarterGear
					print("Tools have saved!")
                end
            end
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        local toolsave = {}
        for i, tool in pairs(player.Backpack:GetChildren()) do
            if tool then
			print("Tools have loaded!")
                table.insert(toolsave,tool.Name)
            end
        end
        datastore:SetAsync("User_"..player.UserId,toolsave)
    end)
end)

The script is just stuck on the search for the tool, it has not printed to show that it has found the tool

1 answer

first of all, you might want to try allowing studio to access API services, since if it can't, it won't be able to use datastores

you can turn that on in the "home" tab in studio, there should be a gear that says "game settings"

second, you need to get rid of this line

toolFound:Clone().Parent = player.StarterGear

players don't have a StarterGear folder, so once one tool gets cloned into their backpack, the script will error and not continue

2 comments

0

As I have already mentioned, my API services are enabled. I'll try removing that line of code though

0

This solution didn't work, unfortunately!

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