Give tool when there is no save

Hi, I’ve been working on a simulator game and I can’t make it happen that when a player has no save, the starter tool will be inserted in the players backpack. This is the code I have now for loading the players tools:

local ss = game:GetService("ServerStorage")
local ds = game:GetService("DataStoreService")
local store = ds:GetDataStore("saveStore")
local library = ss:WaitForChild("Tools")


game.Players.PlayerAdded:Connect(function(player)
	
	local ready = false
	
	player.CharacterAdded:Connect(function(char)
		
		local bp = player.Backpack
		local data = nil
		
		if ready == false then
			ready = true
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
			end
		else
			local firstpc = game.ServerStorage.StarterTool["First Pc"] --problem here
			local clone = firstpc:Clone()
			clone.Parent = player.Backpack
		end

The backpack saving and loading works perfectly fine but I don’t know how to give the tool to the player when he has no save yet

It looks like your else statement that you give the tool to the player in is corresponding to the if ready == false, not the if data then. I think you need to make it like this:

game.Players.PlayerAdded:Connect(function(player)
	
	local ready = false
	
	player.CharacterAdded:Connect(function(char)
		
		local bp = player.Backpack
		local data = nil
		
		if ready == false then
			ready = true
			
			data = store:GetAsync(player.UserId)
			
			if data then
				setup(player, data)
				edit(player, data)
		    else
			    local firstpc = game.ServerStorage.StarterTool["First Pc"] --problem here
			    local clone = firstpc:Clone()
			    clone.Parent = player.Backpack
		    end
        end

This may be wrong, someone please correct me if it is.

You also may want to put your GetAsync in a pcall

Im also kind of confused why you have the ready variable in the first place.

1 Like

idk either it was in the tutorial but thanks that worked!

1 Like