Pickup tool on keypress makes the tool.instance buffer?

I am sorry for this confusing title, I don’t know how to exactly explain my issue but basically I need to parent tool to players backpack, but It parents every tool that was ever caught on raycast.

Just watch the video to get better understanding of what I mean:

The whole messy local script:

local players = game:GetService("Players")
local char = players.LocalPlayer.Character
local root = char:WaitForChild("HumanoidRootPart")
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local label = players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt")
local runservice = game:GetService("RunService")
local tweenservice = game:GetService("TweenService")
local tinfo = TweenInfo.new(0.3, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0)
local TweenOut = tweenservice:Create(label ,tinfo, {TextTransparency=1, TextStrokeTransparency=1})
local tweener = tweenservice:Create(label ,tinfo, {TextTransparency=0, TextStrokeTransparency=0})
local event = game:GetService("ReplicatedStorage").Events.pickup

local raycastParams =  RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {players.LocalPlayer.Character, workspace.Baseplate, workspace.mapstuff}

local function MouseRaycast()
	local mousePos = uis:GetMouseLocation()
	local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 10, raycastParams)
	return rayResult
end

runservice.Heartbeat:Connect(function()
	local result = MouseRaycast()
	if result and result.Instance then
		tweener:Play()
		label.Text = result.Instance.Name
		if result.Instance.Name ~= label.Text then
			TweenOut:Play()
			label.Text = ""
			else
		end
	else
		label.Text = ""	
		TweenOut:Play()
	end
	
	if result and result.Instance.Parent:IsA("Tool") then
		local tool = result.Instance.Parent
		label.Text = tool.Name
		
		for i,v in pairs(char:GetChildren()) do
			if v:IsA("BasePart") and v.Name == "Right Arm" then
				local ruka = v

				uis.InputBegan:Connect(function(iput, prcsd)
					if iput.KeyCode == Enum.KeyCode.E and not prcsd then
						task.wait(0.2)
						event:FireServer(tool)
					end
				end)
			end
		end
	end
end)

Here’s the serverscript:

local event = game:GetService("ReplicatedStorage").Events.pickup

event.OnServerEvent:Connect(function(plr, tool)
	tool.Parent = plr.Backpack
end)
uis.InputBegan:Connect(function(iput, prcsd)
					if iput.KeyCode == Enum.KeyCode.E and not prcsd then
						task.wait(0.2)
						event:FireServer(tool)
					end
				end)

That’s because this event connection is being repetitively created, avoid creating multiple event connections.

local players = game:GetService("Players")
local char = players.LocalPlayer.Character
local root = char:WaitForChild("HumanoidRootPart")
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local label = players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("guistuff"):WaitForChild("WhatYouLookinAt")
local runservice = game:GetService("RunService")
local tweenservice = game:GetService("TweenService")
local tinfo = TweenInfo.new(0.3, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0)
local TweenOut = tweenservice:Create(label ,tinfo, {TextTransparency=1, TextStrokeTransparency=1})
local tweener = tweenservice:Create(label ,tinfo, {TextTransparency=0, TextStrokeTransparency=0})
local event = game:GetService("ReplicatedStorage").Events.pickup

local connection

local raycastParams =  RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {players.LocalPlayer.Character, workspace.Baseplate, workspace.mapstuff}

local function MouseRaycast()
	local mousePos = uis:GetMouseLocation()
	local mouseRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
	local rayResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 10, raycastParams)
	return rayResult
end

runservice.Heartbeat:Connect(function()
	local result = MouseRaycast()
	if result and result.Instance then
		tweener:Play()
		label.Text = result.Instance.Name
		if result.Instance.Name ~= label.Text then
			TweenOut:Play()
			label.Text = ""
		else
		end
	else
		label.Text = ""	
		TweenOut:Play()
	end

	if result and result.Instance.Parent:IsA("Tool") then
		local tool = result.Instance.Parent
		label.Text = tool.Name

		for i,v in pairs(char:GetChildren()) do
			if v:IsA("BasePart") and v.Name == "Right Arm" then
				local ruka = v
				
				if connection then return end
				connection = uis.InputBegan:Connect(function(iput, prcsd)
					if iput.KeyCode == Enum.KeyCode.E and not prcsd then
						task.wait(0.2)
						event:FireServer(tool)
					end
				end)
			end
		end
	end
end)
1 Like

It helped with the tool instance being printed many times at once, but now you can pick up the tool raycast has hit first. I need it to pickup the item if the item is looked at.

nvm, fixed the code by adding some code to my script. If someone has same issue here’s the code:

uis.InputBegan:Connect(function(input, busy)
	if busy then return end
	if input.KeyCode == Enum.KeyCode.E then 
		local tool = MouseRaycast()
		if tool and tool.Instance.Parent:IsA("Tool") then
			
			event:FireServer(tool.Instance.Parent)
		end
	end
end)