So I have this local script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local range = 10

local remote1 = game:GetService("ReplicatedStorage"):WaitForChild("Remote1")

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")
					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")
					end)
				end	
				
			end
		else
			canPickup = false
		end
		
	else
		canPickup = false
	end
	
end

What it should do it should fire a server to remove the part that was clicked from the server and put a tool in player's backpack.

Here is a server script:


local replicatedStorage = game:GetService("ReplicatedStorage")
local remote1 = replicatedStorage:WaitForChild("Remote1")

remote1.OnServerEvent:Connect(function(player, item)
	item:Remove()
	
	local drink = replicatedStorage:WaitForChild("drinkTool"):Clone()
	local food = replicatedStorage:WaitForChild("foodTool"):Clone()
	
	print("item removed from the server")
	if item.Name == "Drink" then
		drink.Parent = player.Backpack
	elseif item.Name == "Food" then
		food.Parent = player.Backpack
	end
end)

It does remove the part from the workspace but the problem is anytime I click with a mouse it gives me like 10 of those tools in my backpack. I think it fires a server every time I click with a mouse so that's why I tried to put that bool value canPickup but I have the same problem.

Any help or suggestion would be appreciated

2 comments

0

btw I can only make variables for food and drinks with mouse.Target because those parts get cloned from the replicated storage and I don't know the exact number of the parts in workspace

0

I would remove the mouse.Button1Down from the loop first because it could fire it many time a second