Inventory Problem - Mouse.Target = nil?

I haven’t scripted for around 2 years because I have been busy, and I have a problem.

So basically I am making an inventory pick system.

Everything works the first time, but the second time, it doesn’t work? It keeps saying you can’t play the the tween because the instance is nil.

Here’s the local script.

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


local uis = game:GetService("UserInputService")




uis.InputChanged:Connect(function(input)
	if mouse.Target then
	if mouse.Target:FindFirstChild("Pick") then

		

			game.Players.LocalPlayer.PlayerGui.InfoGui.Adornee = mouse.Target
			game.Players.LocalPlayer.PlayerGui.InfoGui.ItemName.Text = mouse.Target.Name .. " - " .. "E"
		else
			game.Players.LocalPlayer.PlayerGui.InfoGui.Adornee = nil
			game.Players.LocalPlayer.PlayerGui.InfoGui.ItemName.Text = ""
			
		end
	end
	end)





uis.InputEnded:Connect(function(input)

		if mouse.Target then
			if mouse.Target:FindFirstChild("Pick") then
				
				

					if game.Players.LocalPlayer:DistanceFromCharacter(mouse.Target.Position) < 20 then
					if input.KeyCode == Enum.KeyCode.E then
				
					
						if mouse.Target then
						
							game.ReplicatedStorage:WaitForChild("PickUp"):FireServer(mouse.Target)
							
						wait(.5)
						
					
						
							end
					
				end
			end
		end
	end
end)

Here’s the server script.

local info = TweenInfo.new(
	.5,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0

)

local tweenservice = game:GetService("TweenService")

game.ReplicatedStorage:WaitForChild("PickUp").OnServerEvent:Connect(function(player,item)
	print(item)
	
	local goal = {

	}
	goal.Position = game.Workspace[player.Name].HumanoidRootPart.Position
	goal.Size = Vector3.new(0,0,0)
	if item then

	


	local tween = tweenservice:Create(item,info,goal)
	
	item.CanCollide = false	
	tween:Play()
	
	
	wait(.5)
	
		player.PlayerGui.InfoGui.Adornee = nil
	item:Destroy()
		
		if player.PlayerGui.InventoryFolder[item.Name].Value == 0 then
			player.PlayerGui.InventoryFolder[item.Name].Value = player.PlayerGui.InventoryFolder[item.Name].Value + 1
			local frameclone = game.ServerStorage.ItemFrame:Clone()
			frameclone.Parent = player.PlayerGui.Inventory.InventoryFrame
			frameclone.Name = item.Name 
		
			frameclone.ItemName.Text = item.Name .. "s"
			frameclone.ImageButton.ItemValue.Text = player.PlayerGui.InventoryFolder[item.Name].Value
		
			frameclone.Visible = true
		else
			if player.PlayerGui.InventoryFolder[item.Name].Value > 1 then
				player.PlayerGui.InventoryFolder[item.Name].Value = player.PlayerGui.InventoryFolder[item.Name].Value + 1
				player.PlayerGui.Inventory.InventoryFrame[item.Name].ImageButton.ItemValue.Text = player.PlayerGui.InventoryFolder[item.Name].Value
				player.PlayerGui.Inventory.InventoryFrame[item.Name].ImageButton.Image = game.ServerStorage.ImageList[item.Name].Image
end
		end

end

	
end)

Everything works perfecly except for the line where the tween is created.

I guess the item is “nil” the second time? Not sure how because the mouse always has a target. Does anyone know how to fix this?

Use a RemoteFunction instead to share the mouse target, RemoteEvents don’t have the ability to share mouse properties like Target and Hit.

I’ll try it but how come it works the first time? And no the same thing happened this time.

Thing is, this only happens when I have code to make a clone, and place it in the workspace after I press the drop button, then I can’t pick the item I dropped up…

Nevermind I fixed it by redesigning the whole thing

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.