Touch event not working

when the player touch the part, the script will check if the CurrentSword folder is empty, if it is not empty, it clones the sword that is in the folder and if it is empty, clones the classic sword.

The problem is that: it is printing “not Nil”, but the folder is empty.

Does anyone know why?

script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player then
		local current = player:WaitForChild("CurrentSword")	
		
		if current ~= nil then
			print("not nil")
		
			for i, v in pairs(current:GetDescendants()) do
			if v:IsA("Tool") then
				if not player.Backpack:FindFirstChild(v.Name) then
					if not player.Character:FindFirstChild(v.Name) then
						v:Clone().Parent = player.Character
					end
				end
			end
			end	
		else
			
			local swords = game.ReplicatedStorage.SHOP.Swords

			if not player.Backpack:FindFirstChild("ClassicSword") then
				if not player.Character:FindFirstChild("ClassicSword") then
					local clone = swords:FindFirstChild("ClassicSword"):Clone()
					clone.Parent = player.Character
				end
			end		
		end
   end
end)

The problem is that current, which is a folder, is something (not nil). The script isn’t checking if there’s anything in the folder, only if it exists. Use the GetChildren function to get a table of the children, which will be nil if there are none (I think).

ohhh. of course. Ty !!! Already fixed it

1 Like