So I have a map that randomly spawns in and when players get teleported to the map multiple parts randomly appear as they are cloned from the replicated storage (this works properly). This local script constantly check for magnitude between the player and one of those parts and if a player is in range with the part and clicks on a part, the part will disappear and the tool clone from replicated storage will be parented to player's backpack.
This works fine but only once. When I click another part with the same name it does remove it but I don't get the tool in my backpack
Here's a script:
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local range = 10
local remote1 = game:GetService("ReplicatedStorage"):WaitForChild("Remote1")
local drinkt = game:GetService("ReplicatedStorage"):WaitForChild("drinkTool"):Clone()
local foodt = game:GetService("ReplicatedStorage"):WaitForChild("foodTool"):Clone()
while wait() do
local canPickup = false
if mouse.Target then
if mouse.Target.Name == "Drink" then
print("drink")
local drink = mouse.Target
if (plr.Character.UpperTorso.Position - drink.Position).magnitude < range then
print("in range with drink")
canPickup = true
if canPickup == true then
mouse.Button1Down:Connect(function()
canPickup = false
print("item clicked")
remote1:FireServer(drink)
drink:Remove()
print("item removed from the client")
drinkt.Parent = plr.Backpack
end)
end
end
elseif mouse.Target.Name == "Food" then
print("food")
local food = mouse.Target
if (plr.Character.UpperTorso.Position - food.Position).magnitude < range then
print("in range with food")
canPickup = true
if canPickup == true then
mouse.Button1Down:Connect(function()
canPickup = false
print("item clicked")
remote1:FireServer(food)
food:Remove()
print("item removed from the client")
foodt.Parent = plr.Backpack
end)
end
end
else
canPickup = false
end
else
canPickup = false
end
end
I think it's because the tool variable is cloned only once but again if I put those variables in this while true do loop then when I click on a part I get like 10 of those tools.
Any help is appreciated!

2 comments
I tried this but when I click on the part I get like 10 of those tools in my backpack. And when I tried to check if a player already has an item in it's backpack then whenever I click I get 2 items
try adding a cooldown. script.Disabled = true wait(1) script.Disabled = false